[英]Handle cycle reference durint json serialization/deserialization
我用杰克逊1.9.9开发了一个REST接口。 我有这两个类,并且Parent类包含Child类的列表,而Child类具有对Parent类的引用。 (见下文)
我希望能够将它们序列化为json,并且还需要从序列化的json中反序列化同一对象。
我试过的
public class Child {
private Parent parent;
private Integer id;
private Integer baseId;
public Child(Parent parent, int i) {
this.parent = parent;
this.id = (i+4) * 33;
this.baseId = i;
}
public Integer getBaseId() {
return baseId;
}
public void setBaseId(Integer baseId) {
this.baseId = baseId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JsonBackReference("employer-employee")
public Parent getParent() {
return parent;
}
@JsonBackReference("employer-employee")
public void setParent(Parent parent) {
this.parent = parent;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Child other = (Child) obj;
if (this.parent != other.parent && (this.parent == null || !this.parent.equals(other.parent))) {
return false;
}
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
return false;
}
if (this.baseId != other.baseId && (this.baseId == null || !this.baseId.equals(other.baseId))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + (this.parent != null ? this.parent.hashCode() : 0);
hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
hash = 97 * hash + (this.baseId != null ? this.baseId.hashCode() : 0);
return hash;
}
}
public class Parent {
private Integer id;
private List<Child> list;
public Parent(Integer id){
this.id = id;
list= new ArrayList<Child>();
for(int i=0; i<5; ++i){
list.add(new Child(this, i));
}
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JsonManagedReference("employer-employee")
public List<Child> getList() {
return list;
}
@JsonManagedReference("employer-employee")
public void setList(List<Child> list) {
this.list= list;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Parent other = (Parent) obj;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
return false;
}
if (this.list!= other.list&& (this.list== null || this.list.hashCode() != other.list.hashCode())) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + (this.id != null ? this.id.hashCode() : 0);
hash = 67 * hash + (this.list!= null ? this.list.hashCode() : 0);
return hash;
}
}
编辑:平等的方法固定,从现在起不导致stackoverflow
我关注了这篇文章: http : //wiki.fasterxml.com/JacksonFeatureBiDirReferences
在and处,它说我可能希望将注释同时放在setter和getget上。
我从序列化Parent获得的Json:new Parent(6);
{
"id": 6,
"list": [{
"id": 132,
"baseId": 0
}, {
"id": 165,
"baseId": 1
}, {
"id": 198,
"baseId": 2
}, {
"id": 231,
"baseId": 3
}, {
"id": 264,
"baseId": 4
}]
}
我得到的错误:
在类型[简单类型,类servicetest.Parent]上找不到合适的构造函数:无法从JSON对象实例化(需要添加/启用类型信息?),位于[来源:org.apache.catalina.connector.CoyoteInputStream@11b314c4; 行:1,列:2]
我的问题:
有什么办法可以通过JsonManagedReference和JsonBackReference解决我的问题吗? 有什么方法可以解决此问题,而无需编写自定义序列化(将Child类的父字段序列化为Long,并且在反序列化时,尝试从服务器找到它并连接对象)。这将是最糟糕的方法认为)
谢谢
上面的代码存在问题,即没有默认构造函数。 也将默认构造函数设置为Parent和Child:
Parent.java:
public class Parent {
//...
public Parent(){}
//...
}
Child.java:
public class Child{
//...
public Child(){}
//....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.