我正在开发一个用Spring soap webservice
编写的SOAP Web服务应用程序应用程序只返回从客户端收到的同一对象作为响应。
在CountryEndpoint.java
中@PayloadRoot(namespace = "http://xml.dexter.com/Country", localPart = "CountryLocalPart")
public Country getCountry(SoapHeader soapHeader, @RequestPayload Country request, @SoapHeader(value = "MessageHeader") SoapHeaderElement messageHeader, MessageContext messageContext) {
System.out.println("THE REQUEST: "+request);
return request;
}
请求SOAP XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<ns2:Country xmlns="http://example.org" xmlns:ns2="http://xml.dexter.org/Country" xmlns:h="http://pqr.com/pqr.xsd xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<ns2:countryName>Spain</ns2:countryName>
<ns2:population>1213123</ns2:population>
</ns2:Country>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
响应SOAP XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<ns2:Country xmlns:ns2="http://xml.dexter.com/Country">
<ns2:countryName>Spain</ns2:countryName>
<ns2:population>1213123</ns2:population>
</ns2:Country>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Country XML Root元素似乎缺少xmlns属性。
这导致客户端中的JAXB解组步骤失败。
错误是
ERROR: org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].
[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in
context with path [] threw exception [Request processing failed; nested
exception is org.springframework.oxm.UnmarshallingFailureException: JAXB
unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException:
unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/",
local:"Header"). Expected elements are <{http://www.....}Country>,<{http:
//www.....}Category>,<{http://www.....}SomeOther>] with root cause
javax.xml.bind.UnmarshalException: unexpected element
(uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Header"). Expected
elements are ...
我尝试使用@Namespace数组添加@PayloadRoots和@Namespaces,但没有任何效果。
有没有办法让响应SOAP xml与请求SOAP xml相同?
谢谢!
答案 0 :(得分:2)
请求中Country
元素中带有前缀h
,s
和默认值的名称空间URI是多余的,因为它们与任何XML元素或属性都不对应。没有理由将它们发送给响应,它们会影响JAXB解析结果的能力,假设所有内容都已正确映射。
根据您的例外情况而定。您的JAXB客户端正在尝试解组Header
元素,您需要解组Country
元素。