i have next pojo:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "domain")
@Entity
class Domain {
@ManyToOne
private Domain parent;
@OneToMany //add column definitions as needed
private List<Domain> subdomains;
}
and rs service:
@GET
@Path("/domains/{id}")
@Produces("application/json")
public Bus getDomains(@PathParam("uuid") String uuid){
return domainsService.getByUuid(uuid);
}
when I try to access a resource, I get a loop and
java.lang.StackOverflowError
at com.sun.xml.txw2.StartTag.addNamespaceDecl(StartTag.java:158)
at com.sun.xml.txw2.StartTag.getPrefix(StartTag.java:257)
at com.sun.xml.txw2.DatatypeWriter$1$5.print(DatatypeWriter.java:114)
at com.sun.xml.txw2.DatatypeWriter$1$5.print(DatatypeWriter.java:109)
at com.sun.xml.txw2.Document.writeValue(Document.java:165)
at com.sun.xml.txw2.StartTag.addAttribute(StartTag.java:135)
at com.sun.xml.txw2.ContainerElement._attribute(ContainerElement.java:318)
at com.sun.xml.txw2.ContainerElement._attribute(ContainerElement.java:313)
how to properly transform classes that contain references to themselves?
You are getting into an infinite loop, because when your subdomain tries to render its parent the parent then renders all its subdomains where each subdomain will try to render parent again and so on and so forth until you run out of memory. So, to solve your problem add annotation @JsonIgnore
over your parent
property in your Domain
class