Jackson json双向对象参考 [英] Jackson json two way object reference
问题描述
我有一对像
public class Obj1 {
public int id;
public String name;
public Obj2 obj2;
}
public class Obj2 {
public int id;
public String name;
public List<Obj1> obj1list;
}
我希望能够通过Jackson将其转换为Json。我找到了 JsonManagedReference
和 JsonBackReference
并对它们进行了注释,但是当你这样做时,序列化只能以一种方式工作。它只会显示何时序列化 JsonManagedReference
侧的类。
I want to be able to convert this to Json via Jackson. I found the JsonManagedReference
and JsonBackReference
and annotated them but when you do that, the serialization only works in one way. It will only show when the class with the JsonManagedReference
side is serialized.
如果我序列化Obj1我想看到附在它上面的Obj2。如果我序列化Obj2,我想看到附加到它的Obj1列表。
If I serialize an "Obj1" I want to see the "Obj2" that is attached to it. And if I serialize the "Obj2" I want to see the list of "Obj1"s that is attached to it.
我也尝试过像这样使用JsonIdentityInfo注释
I also tried using JsonIdentityInfo annotation like so
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
这似乎有效,除了它将父对象的id值添加到子对象(或列表大小写中的每个子对象)中,这有点奇怪。虽然我想我可以忍受它。
and this seems to work except that it adds the "id" value of the parent object into the subobject (or every subobject in the list case) which is a bit odd. Though I guess I can live with it.
有没有办法让它按照我的预期行事?
Is there a way to get this to behave as I expect?
推荐答案
您正在查看 @JsonIgnoreProperties
,它将提供所需内容并避免json递归。
You are looking at @JsonIgnoreProperties
, it will give what is needed and avoid json recursion.
public class Obj1 {
public int id;
public String name;
@JsonIgnoreProperties("obj1list")
public Obj2 obj2;
}
public class Obj2 {
public int id;
public String name;
@JsonIgnoreProperties("obj2")
public List<Obj1> obj1list;
}
更新
我总是使用 @JsonIgnoreProperties
超过 JsonBackReference
和 JsonManagedReference
。因为它不仅可以解决问题,而且还不会逃避任何数据序列化(这是必需的)。
UPDATE
I always perfers @JsonIgnoreProperties
over JsonBackReference
and JsonManagedReference
. As it not only does the trick, but also not escapes any data serialization (which is the case required here).
我还有一个关于 github 处理这种情况。查找实体类代码 School.java
& District.java
。它是一个mvn spring-boot可执行代码,如果你想进行测试。
I also have a sample project on github deals with this situation. Look for entity class codes School.java
& District.java
. Its an mvn spring-boot executable code, if you wanna run a test.
来自Javadoc,从Jackson 2.0开始,
@JsonIgnoreProperties
注释既可以应用于类,也可以应用于属性。如果两者都使用,则实际集合将是所有忽略的并集:即,您只能添加要忽略的属性,而不是删除或覆盖。因此,您无法使用每个属性注释删除要忽略的属性。
From Javadoc, starting with Jackson 2.0,
@JsonIgnoreProperties
annotation can be applied both to classes and to properties. If used for both, actual set will be union of all ignorals: that is, you can only add properties to ignore, not remove or override. So you can not remove properties to ignore using per-property annotation.
如何工作
在propety级别定义 @JsonIgnoreProperties
时,在序列化/去除化时,它将对refered属性对象起作用。
所以在这里,当解析 Obj1
时,你要求解析器忽略 obj1list
obj2
的属性。类似的,在解析 Obj2
时,它将忽略 Obj $中包含的
obj2
引用c $ c>收藏。所以你的解析将如下所示:
HOW IT WORKS
When you define @JsonIgnoreProperties
at propety level, while serialization/deserization it will work on the refered property object.
So here, when Obj1
is being parsed, you asked parser to ignore obj1list
property of obj2
. And similary, while parsing Obj2
it will ignore contained obj2
references in the Obj
collection. So your parsing will look like below:
Obj1 : {
id : int,
name : string,
Obj2 : {
id : int,
name : string
obj1list : //ignored avoids recursion
}
}
Obj2 : {
id : int,
name : string,
obj1list : [{
id : int,
name : string,
obj2 : //ignored avoids recursion
},
{
id : int,
name : string
obj2 : //ignored avoids recursion
}
]
}
这篇关于Jackson json双向对象参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!