Search code examples
javaxmljaxbunmarshallingmoxy

JaxbUnmarshaller and Package-info doesn't work


i'm trying to unmarshall an xml like this:

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

i'm using that package-info.java to specify the namespace used into soap response:

@XmlSchema(
    namespace="http://schemas.xmlsoap.org/soap/envelope/", 
    elementFormDefault=XmlNsForm.QUALIFIED,
    xmlns={
            @XmlNs(namespaceURI = "http://schemas.xmlsoap.org/soap/envelope/", prefix = "SOAP-ENV"),
            @XmlNs(namespaceURI = "urn:b2bHotelSOAP", prefix = "ns1"),
            @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema", prefix = "xsd"),
            @XmlNs(namespaceURI = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),
            @XmlNs(namespaceURI = "http://schemas.xmlsoap.org/soap/encoding/", prefix = "SOAP-ENC")

    }
)
@XmlAccessorType(XmlAccessType.FIELD)
package com.giove.viaggi.hsw.provider.hotelspro;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

what i need is to unmarshall that soap xml into this bean:

package myPackage;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="Envelope")
public class MyBean {

    @XmlPath("return/availableHotels/item")
    private List<Hotel> hotels;

    public List<Hotel> getHotels(){
        return this.hotels==null?new ArrayList<Hotel>():this.hotels;
    }
}

When i try to unmarshall it using jaxbUnmarshaller it gives me always null for the hotels attribute even if they are present into soap response.

Do i make any mistake?

Can please someone give me an help?

Thanks!


Solution

  • MyBean

    Below is what the @XmlPath annotation on your MyBean class should look like. Some things to note:

    1. Your domain class must be in the same package as the package-info class you want applied to it.
    2. When you use @XmlPath you need to include each step in the path, you had left some out.
    3. Namespace qualfifcation in the @XmlPath corresponds to the prefixes you have defined in the @XmlSchema annotation (see: http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html).
    package com.giove.viaggi.hsw.provider.hotelspro;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="Envelope")
    public class MyBean {
    
        @XmlPath("SOAP-ENV:Body/ns1:getAvailableHotelResponse/return/availableHotels/item")
        private List<Hotel> hotels;
    
        public List<Hotel> getHotels(){
            return this.hotels==null?new ArrayList<Hotel>():this.hotels;
        }
    
    }
    

    Note

    Instead of mapping the MyBean class to the Envelope element I would map it to the local root return and pass it that element to unmarshal instead.