Web服务 – Spring Web-Service解组无法正常工作
|
我已经像这样配置了我的WebService:
的applicationContext: <sws:annotation-driven />
<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" >
<property name="interceptors">
<list>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
</list>
</property>
注意:Interceptor在启动时加载,但如果有请求,则不会写任何内容. 我有一个PersonServiceImpl类,方法是addPersonRequest().如果我使用org.dom4j.Element作为方法参数,一切正常; @Endpoint
public class PersonServiceImpl {
@PayloadRoot(namespace = "http://www.example.org/person/schema",localPart = "AddPersonRequest")
@ResponsePayload
public AddPersonRequest addPersonRequest(@RequestPayload Element element) {
System.out.println(element.asXML());
Person response = new Person();
response.setId(2);
response.setFirstName("Mad");
response.setLastName("Mike");
return response;
}
}
但是,如果我改变我的方法参数,如下所示(因此应该使用spring-ws的自动编组),request.getFirstName()打印为null. (JAXB2在类路径上). Person-class使用@XMLType和@XMLRootElement进行注释. 注意:编组工作正常. @Endpoint
public class PersonServiceImpl {
@PayloadRoot(namespace = "http://www.example.org/person/schema",localPart = "AddPersonRequest")
@ResponsePayload
public AddPersonRequest addPersonRequest(@RequestPayload Person request,SoapHeader header) {
System.out.println(header.getName());
System.out.println(request.getFirstName());
Person response = new Person();
response.setId(2);
response.setFirstName("Mad");
response.setLastName("Mike");
return response;
}
}
Person.java: @XmlType
@XmlRootElement(namespace="http://www.example.org/person/schema",name="Person")
public class Person implements Serializable {
private int id;
private String firstName;
private String lastName;
@XmlElement
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlElement
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
通过soapUI发送的测试请求(从wsdl生成): <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://www.example.org/person/schema">
<soapenv:Header/>
<soapenv:Body>
<sch:AddPersonRequest>
<sch:Person sch:Id="1">
<sch:FirstName>firstname</sch:FirstName>
<sch:LastName>lastname</sch:LastName>
</sch:Person>
</sch:AddPersonRequest>
</soapenv:Body>
</soapenv:Envelope>
解决方法
你提到编组正在工作我没有看到为什么解组不起作用的原因,你是否测试了马歇尔和解散在isloation?
只是为了确保soap请求是好的,你可以添加日志拦截器并打印来自客户端访问Web服务的实际请求,将此片段添加到您的上下文文件 <sws:interceptors>
<bean class="org.springframework.ws.soap.server.endpoint.interceptor.SoapEnvelopeLoggingInterceptor">
<property name="logRequest" value="true"></property>
<property name="logResponse" value="true"></property>
</bean>
</sws:interceptors>
您应该看到这样的日志消息包含整个soap请求,我正在从saop UI添加日志消息请求 DEBUG [http-8080-2]:endpoint.interceptor.SoapEnvelopeLoggingInterceptor.logMessage - Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://hoost:port/context/patient/schemas">
<soapenv:Header/>
<soapenv:Body>
<sch:addRequest>
<sch:patient>
<sch:id>?</sch:id>
<sch:lastname>Joe</sch:lastname>
</sch:patient>
</sch:addRequest>
</soapenv:Body>
</soapenv:Envelope>
更新的答案 这可能是你的肥皂要求不确定 <sch:FirstName>firstname</sch:FirstName> (this should be)
<sch:firstName>firstname</sch:firstName>
另一个更新 例外是由于您定义终点的方式,在您的soap请求(sch:AddPersonRequest)中,您发送的是addPersonRequest而不是Person作为有效负载,因此更改终点以反映出来,@ RequestPayload应该是AddPersonRequest而不是Person @PayloadRoot(namespace = "http://www.example.org/person/schema",localPart = "AddPersonRequest")
@ResponsePayload
public AddPersonRequest addPersonRequest(@RequestPayload AddPersonRequest request,SoapHeader header) {
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
