while trying to call a web service with the explained structure, such an error pops up at the client side:
com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML:
Parent.java
package deepxml;
import java.util.ArrayList;
import java.util.List;
public class Parent {
private String name;
private List childList = new ArrayList();
public List getChildList() {
return childList;
}
public void setChildList(List childList) {
this.childList = childList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Child.java
package deepxml;
public class Child {
private String name;
private Parent myParent;
public Parent getMyParent() {
return myParent;
}
public void setMyParent(Parent myParent) {
this.myParent = myParent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DeepXml.java
package deepxml.service;
import deepxml.Child;
import deepxml.Parent;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService()
public class DeepXml {
@WebMethod(operationName = "deepXml")
public Parent deepXml(@WebParam(name = "parent")
Parent parent, @WebParam(name = "childList")
List childList) {
parent.setChildList(childList);
for(Child child: childList) {
child.setMyParent(parent);
}
return parent;
}
}
Main.java for client side
package deepxmlclient;
import deepxml.service.Child;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
try { // Call Web Service Operation
deepxml.service.DeepXmlService service = new deepxml.service.DeepXmlService();
deepxml.service.DeepXml port = service.getDeepXmlPort();
deepxml.service.Parent parent = new deepxml.service.Parent();
parent.setName("Parent");
java.util.List childList = new ArrayList();
Child child1 = new Child();
child1.setMyParent(parent);
child1.setName("Child1");
Child child2 = new Child();
child2.setMyParent(parent);
child2.setName("Child2");
parent.getChildList().add(child1);
parent.getChildList().add(child2);
deepxml.service.Parent result = port.deepXml(parent, childList);
System.out.println("Result = "+result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The bold lines in the client code results in the specified error, if you try with those lines commented out, there will be no problem at all.. Lucyly, this works in our situation :)
However, this may not be the correct solution in all cases, so lets try to find a real solution..
Here is the offered solution to the condition: https://jaxb.dev.java.net/guide/Mapping_cyclic_references_to_XML.html
Hiç yorum yok:
Yorum Gönder