我的 REST 服务有问题。问题是我的实体之间有关系,当这个实体将被写为 JSON 时,JBoss 不能这样做。这是代码:
儿童班:
@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;
}
}
家长班:
@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;
}
}
休息方法
@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();
}
JBoss 生成这个损坏的 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":
我怎样才能忽略孩子上的“父母”字段?有没有办法在不重写我的实体的情况下做到这一点,例如“自定义作家”或类似的东西?