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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:16:26  来源:igfitidea点击:

Difference between @JsonIgnore and @JsonBackReference, @JsonManagedReference

javajsonHymanson

提问by Kalyan Pradhan

I know both @JsonIgnoreand @JsonManagedReference, @JsonBackReferenceare 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 @JsonIgnorewill just set null to the field so in our example parentPlayer will be == null.

这是主要区别,因为反序列化@JsonIgnore只会将字段设置为 null,因此在我们的示例中 parentPlayer 将为 == null。

enter image description here

在此处输入图片说明

But with @JsonManagedReference+ @JsonBackReferencewe will get Inforeferance there

但是使用@JsonManagedReference+@JsonBackReference我们将在Info那里获得参考

enter image description here

在此处输入图片说明

回答by Ali Dehghani

are used to solve the Infinite recursion (StackOverflowError)

用于解决无限递归(StackOverflowError)

@JsonIgnoreis 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 @JsonIgnoreignores the annotated property, you may avoid the infinite recursion.

@JsonIgnore不是为了解决无限递归问题而设计的,它只是忽略了被序列化或反序列化的注释属性。但是如果字段之间存在双向链接,由于@JsonIgnore忽略了注释属性,您可以避免无限递归。

On the other hand, @JsonManagedReferenceand @JsonBackReferenceare designed to handle this two-way linkage between fields, one for Parentrole, the other for Childrole, respectively:

另一方面,@JsonManagedReferenceand@JsonBackReference旨在处理字段之间的这种双向链接,一个用于角色,另一个用于角色:

For avoiding the problem, linkage is handled such that the property annotated with @JsonManagedReferenceannotation is handled normally (serialized normally, no special handling for deserialization) and the property annotated with @JsonBackReferenceannotation 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/@JsonBackReferencepair is the way to go.

回顾一下,如果您在序列化或反序列化过程中不需要这些属性,则可以使用@JsonIgnore. 否则,使用@JsonManagedReference/@JsonBackReference对是可行的方法。