我用杰克逊 1.9.9 开发了一个 REST 接口。我有这两个类,父类包含子类列表,子类有对父类的引用。(见下文)
我希望能够将它们序列化为 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 和 gettes 上。
我从序列化 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
}]
}
我得到的错误:
没有找到适合类型 [simple type, class servicetest.Parent] 的构造函数:无法在 [Source: org.apache.catalina.connector.CoyoteInputStream@11b314c4; 行:1,列:2]
我的问题:
有什么方法可以解决我的 JsonManagedReference 和 JsonBackReference 问题吗?有没有办法解决这个问题,不写自定义序列化(将子类父字段序列化为Long,反序列化时,尝试从服务器找到它并连接对象o它。这将是最糟糕的方法我思考)
谢谢