@JsonIdentityInfo 按预期与以下类一起工作:
基类:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class TestEntityBas {
@JsonProperty
public String uuid = "0001";
}
子类:
public class TestEntityGoa extends TestEntityBas {
@JsonProperty
public String texten = "This is text!";
}
容器类:
public class TestEntity {
@JsonProperty
String stringer = "Hej hopp!";
@JsonIdentityReference(alwaysAsId = true)
public TestEntityGoa goa = new TestEntityGoa();
}
结果符合预期:
{"stringer":"Hej hopp!","goa":"0001"}
当我将 @JsonTypeInfo 添加到基类时,如下所示:
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class TestEntityBas {
@JsonProperty
public String uuid = "0001";
}
现在整个 TestEntityGoa 被序列化如下:
{"stringer":"Hej hopp!","goa":{"@class":"com.fodolist.model.TestEntityGoa","uuid":"0001","texten":"This is text!"}}
即使我在同一个类中使用@JsonTypeInfo和@JsonIdentityInfo,我也期望第一个结果。我做错了什么?
最佳答案
我在这里看不到任何明显的错误,所以您可能发现了一个错误。类型和身份信息的组合处理起来有点棘手,因此可能存在尚未按预期工作的边缘情况,因此您可以在 Github 问题跟踪器上为此提交一个错误吗?
关于jackson - 为什么 @JsonTypeInfo 不与 @JsonIdentityInfo 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15041933/