简体   繁体   English

使用Java中的Contract-First进行Web服务循环

[英]Web Service cycle using Contract-First in Java

I'm working on an application with contract first web services ( wsimport and jaxws-maven-plugin ). 我正在开发一个具有合同优先Web服务的应用程序( wsimportjaxws-maven-plugin )。

How should I write WSDL/XSD files in order to be able to process cycles? 我应该如何编写WSDL / XSD文件以便能够处理循环? For example object department with reference to employees and employee with reference to department (as in this article http://jaxb.java.net/guide/Mapping_cyclic_references_to_XML.html ). 例如,对象department参考employeesemployee参考department (如本文http://jaxb.java.net/guide/Mapping_cyclic_references_to_XML.html )。 The article notes a @XmlTransient annotation, but as I use contract-first, I'm unable to modify generated classes in any way. 本文注意到@XmlTransient注释,但是当我使用contract-first时,我无法以任何方式修改生成的类。

If I just ignore those cycles, first time I run the webservice, I receive an error such as: 如果我只是忽略这些周期,第一次运行webservice时,我收到一个错误,例如:

Caused by: com.sun.istack.SAXException2: 
A cycle is detected in the object graph. This will cause infinitely deep XML: 
Employee@18ac4d8 -> Department@aa35d5 -> Employee@18ac4d8

The problem is that webservice contracts (at least WS-I Basic Profile compliant ones) can not encode references to other objects in the message. 问题是webservice契约(至少符合WS-I Basic Profile)不能编码对消息中其他对象的引用。 That is, a field of reference type is always marshalled by marshalling the fields of the object it refers to. 也就是说,引用类型的字段总是通过编组它所引用的对象的字段来编组。 This recursion is limitless if the object graph contains a cycle. 如果对象图包含循环,则此递归是无限的。

That is, if you had: 也就是说,如果你有:

class A {
    String name;
    A a;
}

and did: 并做了:

A a = new A();
a.name = "hello";
a.a = a;
marshall(a);

The XML would look like XML看起来像

<a>
    <name>hello</name>
    <a>
        <name>hello</name>
        <a>
            <name>hello</name>
            <a>
                ...

To avoid this, the cycle needs to be broken. 为避免这种情况,需要打破循环。 Typical approaches include to make the association navigable in only one direction, set backreferences to null prior to marshalling (tasking the recipient to reconstruct them), move the associations to seperate classes like in 典型的方法包括使关联只能在一个方向上导航,在编组之前将反向引用设置为null (命令接收者重建它们),将关联移动到单独的类中

class A {
    String name;
}
class B {
    String adress;   
}

class AWithB {
    A a;
    B b;
}

and a host of other options. 以及许多其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM