RestTemplate和肥皂unmarshaller

时间:2014-05-26 09:01:47

标签: xml spring soap jaxb moxy

我试图使用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>

可以请别人向我解释一个好的解决方案吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

请不要混合使用SOAP和REST。

尝试从Spring WebServices项目中使用WebServiceTemplate解决您的问题,或者在将XML响应解组为域对象之前对XML Response进行一些XPath转换(//getAvailableHotelResponse)。

这是一个简单的例子:

WebServiceTemplate template = new WebServiceTemplate(marshaller, unmarshaller);
template.marshalSendAndReceive(endpoint.toURI(), entity);