How to resolve Json Infinite Recursion problem when working with Jackson

Json Infinite Recursion is one of the most common problems when we serialize Java objects which having Bidirectional-Relationships. So in the tutorial JavaSampleApproach will guide you how to smoothly handle the problems with Jackson annotations: @JsonIgnore, @JsonView, {@JsonManagedReference, @JsonBackReference} and @JsonIdentityInfo.

Related articles:
Jackson to convert Java Object to/from Json String
Integrate Spring RestAPIs with @JsonView
How to use @JsonView to serialize/de-serialize and customize JSON format from Java Object

I. Infinite Recursion problem

We create 2 model classes: Company & Product have one-to-many relationship:

Company:

Product

Serialize Java Objects with segment code:

-> Infinite recursion (StackOverflowError) thrown:

Why? -> Beacause of the Bidirectional-Relationships between Company objects and Product objects.

II. Solution

Initial an apple Company & 2 Product {iphone7, iPadPro} with segment code:

1. Solution 1 – Exclude one side of the relationship by @JsonIgnore or @JsonView
1.1 Use @JsonIgnore

Modify Product with @JsonIgnore

– Serialize with segment code:

-> Result:

– Deserialize with segment code:

Json Infinite Recursion - Deserialized processing -JSONIGNORE

-> Can not preserve the Bidirectional-Relationships

1.2 Use @JsonView

See How to use @JsonView to serialize/de-serialize and customize JSON format from Java Object.

Modify 2 models Product with @JsonView:

Company:

Product:

– Serialize with segment code:

-> Result:

– Deserialize with segment code:

Json Infinite Recursion - Deserialized processing -JsonView

-> Can not preserve the Bidirectional-Relationships after deserialization.

2. Solution 2 – Use {@JsonManagedReference, @JsonBackReference} and @JsonIdentityInfo

With the solution we can preserve Bidirectional-Relationships when deserialize Json strings to Java objects

2.1 Use @JsonManagedReference, @JsonBackReference

@JsonManagedReference: a part with the annotation will be serialized normally.
@JsonBackReference: a part with the annotation will be omitted from serialization.

Modify 2 models Product with {@JsonManagedReference, @JsonBackReference}:

Company:

Product:

– Serialize with segment code:

-> Result:

– Deserialize with segment code:

Json Infinite Recursion - Deserialized processing -JsonManagedBackReference

-> Can preserve the Bidirectional-Relationships after deserialization.

2.2 Use @JsonIdentityInfo

Modify 2 models Product with @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property="@id", scope = Product.class)

Company:

Product:

– Serialize with segment code:

-> Result:

– Deserialize with segment code:

Json Infinite Recursion - Deserialized processing - @JsonIdentityInfo Debug

-> Can preserve the Bidirectional-Relationships!

1. @JsonIgnore, @JsonView can NOT preserve the Bidirectional-Relationships!
2. {@JsonManagedReference, @JsonBackReference} and @JsonIdentityInfo: can preserve the Bidirectional-Relationships!

III. Sourcecode

Technologies
– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.8.4.RELEASE
– Spring Boot: 1.5.4.RELEASE
– Jackson library

Use below dependency for Jackson library:

SourceCodes:
JacksonJsonIgnore
JacksonJsonView
JacksonJsonManagedBackReference
JacksonJsonJsonIdentityInfo

By JavaSampleApproach | June 26, 2017.


Related Posts


2 thoughts on “How to resolve Json Infinite Recursion problem when working with Jackson”

  1. Good post. I have a question for @JsonIdentityInfo.
    I have tried to serialize Product object. its giving like below:

    But I should get like:

    How to achieve that ?

    1. Hello Thrinath,

      serialize Object To String with the info:

      That’s enough info and not redundancy for deserialize String To Object.
      So Why you want to work-arround with your expected which makes lots of redundancy data and inconsitency?

      Regards,
      JSA

Got Something To Say:

Your email address will not be published. Required fields are marked *

*