I'm having a problem with my REST service. The problem is that I have a relationship between my entities and when this entity is going to be written as a JSON the JBoss can't do that. Here are the code:
Child class:
@XmlRootElement
class Child implements Serializable {
private static final long serialVersionUID = -6058827989172575778L;
private String childName;
private Parent parent;
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
Parent Class:
@XmlRootElement
class Parent implements Serializable {
private static final long serialVersionUID = -8280071521315889541L;
private String parentName;
private List<Child> childs = new ArrayList<Child>();
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
public List<Child> getChilds() {
return childs;
}
public void setChilds(List<Child> childs) {
this.childs = childs;
}
}
REST Method
@GET
@Path("/test")
@Produces("application/json")
public Response test() {
final Parent parent = new Parent();
parent.setParentName("Parent name");
Child child = new Child();
child.setChildName("Child name");
child.setParent(parent);
parent.getChilds().add(child);
ResponseBuilder rb = Response.ok(parent);
return rb.build();
}
The JBoss generates this broken JSON:
{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":{"parentName":"Parent name","childs":[{"childName":"Child name","parent":
How can I ignores the "parent" field on the Child? Is there a way to do that without rewriting my entities, such as a "custom writer" or something like that?