我正在尝试使用Jackson 2.9.8对实体进行序列化和反序列化,并且遇到了这个异常。
我的实体没有id属性,我正在使用父类的hashCode()
方法。
我称之为SerializationId
。
我得到的例外是:
原因:java.lang.IllegalStateException:已经有ID的POJO (java.lang.Integer)[[ObjectId:key = -23395637, 类型= com.palmyra.test.TestIdGeneratorUMLX,范围= UMLX]] 在com.fasterxml.jackson.annotation.SimpleObjectIdResolver.bindItem(SimpleObjectIdResolver.java:24)
在JSON中,我在父级和子级中发现了SerializationId
。
答案 0 :(得分:0)
import java.util.Collection;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@JsonIdentityInfo(generator = MyGenerator.class, property = "SerializationId ", scope = X.class)
public class X extends Z{
private Map<String,Y> maps;
// ... other columns, getters & setters
}
import java.util.Collection;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@JsonIdentityInfo(generator = MyGenerator.class, property = "SerializationId ", scope = Y.class)
public class y extends Z{
private Y myY;
// ... other columns, getters & setters
}
import java.util.Collection;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@JsonIdentityInfo(generator = MyGenerator.class, property = "SerializationId ", scope = Z.class)
public class Z {
private String fullName;
//this method return the hashcode of the fullName which is unique .
public int hashcode(){
}
// ... other columns, getters & setters
}
public class MyGenerator extends ObjectIdGenerator<Z>{
generateId(forPojo){
//getting the hashcode
forPojo.hashcode();
}
}