Best way of handling Jackson bi-directional references

136 views Asked by At

I'm trying to build rest APIs for our core components using Jackson, and I had issues with some of the objects getting this exception:

javax.ws.rs.ProcessingException: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)

After searching I came out to know about several ways how to solve it. e.g. https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

and I used the @JsonIdentityInfo which is working fine for me, but the question is WHAT IS THE BETTER WAY TO DO IT?

In this post: Infinite Recursion with Jackson JSON and Hibernate JPA issue

There is a claim that need to use the @JsonIdentityInfo in caution cause it can cause problems:

In this case you've got to be careful, since you could need to read your object's attributes more than once (for example in a products list with more products that share the same seller), and this annotation prevents you to do so. I suggest to always take a look at firebug logs to check the Json response and see what's going on in your code.

I reached this article as well: http://springquay.blogspot.com/2016/01/new-approach-to-solve-json-recursive.html

@JsonIdentityInfo

I understood that @JsonIdentityInfo is newer approach in Jackson 2.

Advantage that it requires minimum code change (just to put this annotation in the problematic Object Model and no need to handle it from the other side.

A drawback is explained

@JsonIgnoreProperties It requires to change more classes rather than just annotating the base one, and I'm not sure how it will will work if I have more than one class inheriting from that object model.

0

There are 0 answers