【发布时间】:2014-07-15 00:16:04
【问题描述】:
我正在尝试使用 moxy 来解组肥皂响应,例如:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:b2bHotelSOAP" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getAvailableHotelResponse>
<return xsi:type="ns1:getAvailableHotelResponse">
<responseId xsi:type="xsd:integer">1</responseId>
<searchId xsi:type="xsd:string">HR-47754204</searchId>
<totalFound xsi:type="xsd:integer">20</totalFound>
<availableHotels SOAP-ENC:arrayType="ns1:hotel[20]" xsi:type="ns1:hotelArray">
...
</availableHotels>
</return>
</ns1:getAvailableHotelResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是我的豆子:
@XmlRootElement(name = "getAvailablegetAvailableHotelResponse")
public class MyBean{
@XmlPath("return/availableHotels/item")
private List<Hotel> hotels;
public List<Hotel> getHotels(){
return this.hotels==null?new ArrayList<Hotel>():this.hotels;
}
}
使用简单的 xml 字符串,我正确地将 xml 转换为 bean,但在这个肥皂盒中我不知道该怎么做。
为了获得响应,我以这种方式使用 RestTemplate:
MyBean response = template.postForObject(endpoint.toURI(), entity, MyBean.class);
我收到了这个例外:
org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to
[class myPackage.MyBean]: unexpected element
(uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are
<{}getAvailablegetAvailableHotelResponse>; nested exception is
javax.xml.bind.UnmarshalException: unexpected element
(uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are
<{}getAvailablegetAvailableHotelResponse>
谁能给我解释一个好的解决方案?
谢谢!
【问题讨论】:
-
不要使用
RestTemplate,因为这不是休息服务,而是肥皂服务。使用 Spring-WS 中的WebServiceTemplate。