【发布时间】:2016-01-02 17:15:19
【问题描述】:
有没有办法用@JsonIdentityInfo 影响序列化过程,使其插入整个对象而不是引用id?
@Entity
@JsonIdentityInfo(
generator = ObjectIdGenerators.IntSequenceGenerator.class,
property = "linkLabel")
public class LinkLabel implements Serializable {
//...
}
因此,Jackson 不应引用 id 为 1 的“otherObj”,而应包含整个对象。
{
"objects": [{
"id": 1,
"otherObj": [{
"id": 1,
...
}, {
"id": 3,
...
}]
},
"id": 2,
"otherObj": [1] <-- referencing otherObj with id 1
]
}
喜欢这里:
{
"objects": [{
"id": 1,
"otherObj": [{
"id": 1,
...
}, {
"id": 3,
...
}]
},
"id": 2,
"otherObj": [{
"id": 1, <-- desired format, whole object
...
}]
]
}
我们有双向引用,所以@JsonManagedReference 和@JsonBackReference 不能正常工作。此处描述了此行为 (http://wiki.fasterxml.com/JacksonFeatureObjectIdentity)。
【问题讨论】:
-
见github.com/FasterXML/jackson-databind/issues/372
@JsonIdentityInfo isn't meant to do that: it's meant to resolve cyclic dependencies in an object graph (during serialization and deserialization) by using an ID/reference mechanism so that an object instance is only completely serialized once and referenced by its ID elsewhere. So your first point ("making incomplete JSON objects") is an intended behaviour. -
那么,使用双向引用(因此 JsonManagedReference/JsonBackReference 不起作用)以及序列化和反序列化(我找不到让 JsonView 使用反序列化的方法)的最佳解决方案是什么。实体模型相当复杂,所以 JsonIgnore 不是解决方案。
-
抱歉,我没有用过 Jackson...我只是指出一些似乎相关的东西
标签: java json jackson jsonidentityinfo