Java @JsonIgnore 和 @JsonBackReference、@JsonManagedReference 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37392733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between @JsonIgnore and @JsonBackReference, @JsonManagedReference
提问by Kalyan Pradhan
I know both @JsonIgnore
and @JsonManagedReference
, @JsonBackReference
are used to solve the Infinite recursion (StackOverflowError)
, what is the difference between these two?
@JsonIgnore
和@JsonManagedReference
,我都知道,@JsonBackReference
都是用来解决的Infinite recursion (StackOverflowError)
,这两个有什么区别?
Note :These are Hymanson annotations.
注意:这些是 Hymanson 注释。
采纳答案by varren
Lets suppose we have
让我们假设我们有
private class Player {
public int id;
public Info info;
}
private class Info {
public int id;
public Player parentPlayer;
}
// something like this:
Player player = new Player(1);
player.info = new Info(1, player);
Serialization
序列化
@JsonIgnore
@JsonIgnore
private class Info {
public int id;
@JsonIgnore
public Player parentPlayer;
}
and @JsonManagedReference
+ @JsonBackReference
和@JsonManagedReference
+@JsonBackReference
private class Player {
public int id;
@JsonManagedReference
public Info info;
}
private class Info {
public int id;
@JsonBackReference
public Player parentPlayer;
}
will produce same output. And outputfor demo case from above is: {"id":1,"info":{"id":1}}
将产生相同的输出。并输出从上面演示的情况是:{"id":1,"info":{"id":1}}
Deserialization
反序列化
Here is main difference, because deserialization with @JsonIgnore
will just set null to the field so in our example parentPlayer will be == null.
这是主要区别,因为反序列化@JsonIgnore
只会将字段设置为 null,因此在我们的示例中 parentPlayer 将为 == null。
But with @JsonManagedReference
+ @JsonBackReference
we will get Info
referance there
但是使用@JsonManagedReference
+@JsonBackReference
我们将在Info
那里获得参考
回答by Ali Dehghani
are used to solve the Infinite recursion (StackOverflowError)
用于解决无限递归(StackOverflowError)
@JsonIgnore
is not designed to solve the Infinite Recursionproblem, it just ignores the annotated property from being serialized or deserialized. But if there was a two-way linkage between fields, since @JsonIgnore
ignores the annotated property, you may avoid the infinite recursion.
@JsonIgnore
不是为了解决无限递归问题而设计的,它只是忽略了被序列化或反序列化的注释属性。但是如果字段之间存在双向链接,由于@JsonIgnore
忽略了注释属性,您可以避免无限递归。
On the other hand, @JsonManagedReference
and @JsonBackReference
are designed to handle this two-way linkage between fields, one for Parentrole, the other for Childrole, respectively:
另一方面,@JsonManagedReference
and@JsonBackReference
旨在处理字段之间的这种双向链接,一个用于父角色,另一个用于子角色:
For avoiding the problem, linkage is handled such that the property annotated with
@JsonManagedReference
annotation is handled normally (serialized normally, no special handling for deserialization) and the property annotated with@JsonBackReference
annotation is not serialized; and during deserialization, its value is set to instance that has the "managed" (forward) link.
为避免该问题,对链接进行处理,使得注解的属性
@JsonManagedReference
正常处理(正常序列化,反序列化不做特殊处理),@JsonBackReference
不序列化注解的属性;并且在反序列化期间,它的值被设置为具有“托管”(前向)链接的实例。
To recap, if you don't need those properties in the serialization or deserialization process, you can use @JsonIgnore
. Otherwise, using the @JsonManagedReference
/@JsonBackReference
pair is the way to go.
回顾一下,如果您在序列化或反序列化过程中不需要这些属性,则可以使用@JsonIgnore
. 否则,使用@JsonManagedReference
/@JsonBackReference
对是可行的方法。