Jayyrus
Jayyrus

Reputation: 13061

RestTemplate and soap unmarshaller

i'm trying to use moxy to unmarshal a soap response like:

<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>

This is my bean:

@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;
    }
}

Using simple xml string, i correctly convert xml to bean, but in this soap case i don't know how to do.

To get response i use RestTemplate in this way:

MyBean response = template.postForObject(endpoint.toURI(), entity, MyBean.class);

And i receive that exception:

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>

Can please someone explain me a good solution?

Thanks!

Upvotes: 1

Views: 8354

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121462

Please, don't mix SOAP and REST.

Try to solve your issue with WebServiceTemplate from the Spring WebServices project, or do some XPath transformation (//getAvailableHotelResponse) for the XML Response before unmarshalling it to the domain object.

Here's a simple example:

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

Upvotes: 4

Related Questions