Web Service Cycle Using Contract-First in Java

I am working on an application with the first contract web services ( wsimport and jaxws-maven-plugin ).

How do I write WSDL / XSD files to be able to handle loops? For example, a department object with a link to employees and employee with a link to department (as in this article http://jaxb.java.net/guide/Mapping_cyclic_references_to_XML.html ). The article notes the @XmlTransient annotation, but since I use the contract first, I cannot modify the generated classes in any way.

If I just ignore these loops, the first time I start webservice, I get an error, for example:

 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 
+4
source share
1 answer

The problem is that webservice contracts (at least compatible with the WS-I base profile) cannot encode links to other objects in the message. That is, a field of a reference type is always sorted by the marshalls of the fields of the object to which it refers. This recursion is infinite if the object graph contains a loop.

That is, if you had:

 class A { String name; A a; } 

and did:

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

XML will look like

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

To avoid this, the cycle must be broken. Typical approaches are to make the association available in only one direction, set a null backlink before marshalling (instruct the recipient to restore them), move the associations to separate classes, for example,

 class A { String name; } class B { String adress; } class AWithB { A a; B b; } 

and many other options.

+3
source

Source: https://habr.com/ru/post/1338729/


All Articles