64

I know both @JsonIgnore and @JsonManagedReference, @JsonBackReference are used to solve the Infinite recursion (StackOverflowError), what is the difference between these two?

Note : These are Jackson annotations.

Kalyan Pradhan
  • 1,415
  • 3
  • 19
  • 34
  • 1
    Yes I have tried out the documents and implemented it in the project but I was only confused with the technique they use in the process of conversion. As both of them produced the same result so I was confused where to prefer one over the other. – Kalyan Pradhan May 23 '16 at 17:44
  • 2
    Three approaches to solve JSON recursive dependency: http://springquay.blogspot.com/2016/01/new-approach-to-solve-json-recursive.html – DurandA Feb 11 '20 at 20:30

2 Answers2

80

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

private class Info {
    public int id;
    @JsonIgnore
    public Player parentPlayer;
}

and @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 output for demo case from above is: {"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.

enter image description here

But with @JsonManagedReference + @JsonBackReference we will get Info referance there

enter image description here

Mukus
  • 4,870
  • 2
  • 43
  • 56
varren
  • 14,551
  • 2
  • 41
  • 72
  • 3
    Thanks a lot it cleared a lot of doubts in my mind. I was trying to search for this particular type of serialization which will not break or ignore either side of serialization – Kalyan Pradhan May 23 '16 at 17:50
  • @varren I have a similar question [here](https://stackoverflow.com/questions/48469722/how-to-make-pojo-and-parse-recursive-objects-using-jackson) where I need to parse recursive field and looks like maybe I need to use `@JsonManagedReference` but not sure how it's gonna work? I tried using `JsonManagedReference` and `JsonBackReference` but it seems doesn't work and everytime I get an exception so wanted to see if you can help me out. – user1950349 Jan 26 '18 at 22:48
48

are used to solve the Infinite recursion (StackOverflowError)

@JsonIgnore is not designed to solve the Infinite Recursion problem, 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.

On the other hand, @JsonManagedReference and @JsonBackReference are designed to handle this two-way linkage between fields, one for Parent role, the other for Child role, respectively:

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.

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.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • 2
    Thanks Ali this was helpful for me finding out the real difference between ignoring, serializing and de-serializing. Thanks a lot – Kalyan Pradhan May 23 '16 at 17:55
  • @AliDehghani I have a similar question [here](https://stackoverflow.com/questions/48469722/how-to-make-pojo-and-parse-recursive-objects-using-jackson) where I need to parse recursive field and looks like maybe I need to use `@JsonManagedReference` but not sure how it's gonna work? I tried using `JsonManagedReference` and `JsonBackReference` but it seems doesn't work and everytime I get an exception so wanted to see if you can help me out. – user1950349 Jan 26 '18 at 22:49