JaxbUnmarshaller和Package-info不起作用

时间:2014-05-27 07:02:48

标签: java xml jaxb unmarshalling moxy

我试图解开像这样的xml:

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

我使用package-info.java来指定用于soap响应的命名空间:

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

我需要的是将soap xml解组到这个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;
    }
}

当我尝试使用jaxbUnmarshaller对其进行解组时,即使它们存在于soap响应中,它也会让我对hotels属性始终为null。

我犯了什么错吗?

可以请别人帮我一个忙吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

<强>为myBean

以下是@XmlPath课程中的MyBean注释应如下所示。有些事情需要注意:

  1. 您的域类必须与要应用它的package-info类位于同一个包中。
  2. 当您使用@XmlPath时,您需要在路径中包含每个步骤,而您已经留下了一些。
  3. @XmlPath中的命名空间限定符对应于您在@XmlSchema注释中定义的前缀(请参阅:http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html)。
  4. 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;
        }
    
    }
    

    注意

    不是将MyBean类映射到Envelope元素,而是将其映射到本地根return,并将该元素传递给unmarshal。