如何通过JAXB解组.NET Soap Formatter创建的SOAP消息

时间:2017-03-23 11:49:19

标签: java c# wcf soap jaxb

我有一个用C#通过SOAP Formatter制作的SOAP消息:

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <a1:Artifact id="ref-1" xmlns:a1="artifactNamespace">
            <code id="ref-4">TestCode</code>        
            <exam href="#ref-14"/>          
        </a1:CrfArtifact>
        <a4:Exam id="ref-14" xmlns:a4="examNamespace">
            <name id="ref-20">TestExam</name>
        </a4:Exam>  
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我正在尝试创建正确的Java强类型对象,它将遵循此消息。在C#我有hierarhy:

Artifact 
 |
 ------Exam

我希望在Java中拥有相同的功能。正如您在SOAP消息格式器中看到的那样,将Artifact和Exam放在同一级别上,并通过 href-&gt; ref 关系链接它们。我的问题是如何创建这样的数据模型,该模型可以正确地将此消息反序列化为具有所述层次结构的Java对象。

我的信封:

@XmlRootElement(name="Envelope")
@XmlAccessorType(XmlAccessType.FIELD)
public class Envelope {
    @XmlElement(name="Body")
    public Body body;
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
   @XmlElement(name="Artifact")
   public generated.Artifact artifact;
}

我的Java数据类:

@XmlAccessorType(XmlAccessType.FIELD)
public class Artifact {

    @XmlElement(name="code")
    public String examcode;    

    @XmlElement(name="exam")
    public Exam exam; 
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Exam {

    @XmlAttribute(name="id")
    @XmlID
    public String examid;

    @XmlElement(name="name")
    public String examname;
}

我使用NamespaceFilter来删除命名空间以使事情变得更清楚,我的Exam节点没有被unmarshaller拾取并且始终为null:

public static void Unmarshal() throws JAXBException, SAXException, FileNotFoundException {

    //Create context for my message
    JAXBContext jaxbContext = JAXBContext.newInstance(Envelope.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    // Create an XMLReader to use the namespace filter
    XMLReader reader = XMLReaderFactory.createXMLReader();

    // Create the filter (to add namespace) and set the xmlReader as its
    // parent.
    NamespaceFilter inFilter = new NamespaceFilter(null, false);
    inFilter.setParent(reader);

    // Prepare the input
    InputSource is = new InputSource(new FileInputStream("src/main/resources/input.xml"));

    // Create a SAXSource specifying the filter
    SAXSource source = new SAXSource(inFilter, is);

    // Do unmarshalling
    Envelope myJaxbObject = (Envelope) jaxbUnmarshaller.unmarshal(source);
}

结果:

enter image description here

任何帮助都是精神上的。感谢

0 个答案:

没有答案