How to resolve Json Infinite Recursion problem when working with Jackson

resolve-json-infinite-recursion-problems-working-jackson-feature-image

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 show 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:


public class Company {
	private int id;
    private String name;
    private List products;
	
    public Company(){
    }
    ...

Product


public class Product {
	private int id;
    private String name;
    private Company company;
	
    public Product(){
    }

Serialize Java Objects with segment code:


(new ObjectMapper()).writeValueAsString(apple);

-> Infinite recursion (StackOverflowError) thrown:


Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"]->java.util.ArrayList[0]->com.javasampleapproach.jackson.model.Product["company"]->com.javasampleapproach.jackson.model.Company["products"])
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:706)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)

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:


Product iphone7 = new Product(1, "Iphone 7");
Product iPadPro = new Product(2, "IPadPro");

List appleProducts = new ArrayList(Arrays.asList(iphone7, iPadPro));

Company apple = new Company(1, "Apple", appleProducts);

iphone7.setCompany(apple);
iPadPro.setCompany(apple);

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

1.1 Use @JsonIgnore

Modify Product with @JsonIgnore


public class Product {
	private int id;
    private String name;
    
    @JsonIgnore
    private Company company;

– Serialize with segment code:


ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(company);

-> Result:


{
   "id":1,
   "name":"Apple",
   "products":[
      {
         "id":1,
         "name":"Iphone 7"
      },
      {
         "id":2,
         "name":"IPadPro"
      }
   ]
}

– Deserialize with segment code:


ObjectMapper mapper = new ObjectMapper();
Object object = mapper.readValue(jsonString, valueType);

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:


public class Company {
	@JsonView(View.DetailView.class)
	private int id;
	
	@JsonView(View.DetailView.class)
    private String name;

	@JsonView(View.DetailView.class)
    private List products;
	
    public Company(){
    }
	
	...

Product:


public class Product {
	@JsonView(View.DetailView.class)
	private int id;
	
	@JsonView(View.DetailView.class)
    private String name;
    
    private Company company;
	
    public Product(){
    }
	
	...

– Serialize with segment code:


ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
String result = mapper.writerWithView(serializationView).writeValueAsString(object);

-> Result:


{
   "id":1,
   "name":"Apple",
   "products":[
      {
         "id":1,
         "name":"Iphone 7"
      },
      {
         "id":2,
         "name":"IPadPro"
      }
   ]
}

– Deserialize with segment code:


ObjectMapper mapper = new ObjectMapper();
Object object = mapper.readerWithView(view).forType(valueType).readValue(jsonString);

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:


public class Company {
	private int id;
    private String name;
    
    @JsonManagedReference
    private List products;
	
    public Company(){
    }
	
	...

Product:


public class Product {
	private int id;
    private String name;
    
    @JsonBackReference
    private Company company;
	
    public Product(){
    }
	
	...

– Serialize with segment code:


ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(company);

-> Result:


{
   "id":1,
   "name":"Apple",
   "products":[
      {
         "id":1,
         "name":"Iphone 7"
      },
      {
         "id":2,
         "name":"IPadPro"
      }
   ]
}

– Deserialize with segment code:


ObjectMapper mapper = new ObjectMapper();
Object object = mapper.readValue(jsonString, valueType);

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:


@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property="@id", scope = Product.class)
public class Company {
	private int id;
    private String name;
    private List products;
	
    public Company(){
    }
	
	...

Product:


@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property="@id", scope = Product.class)
public class Product {
	private int id;
    private String name;
    
    private Company company;
	
    public Product(){
    }
	
	...

– Serialize with segment code:


ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(company);

-> Result:


Serialize Process - JsonString:{
   "@id":1,
   "id":1,
   "name":"Apple",
   "products":[
      {
         "@id":1,
         "id":1,
         "name":"Iphone 7",
         "company":1
      },
      {
         "@id":2,
         "id":2,
         "name":"IPadPro",
         "company":1
      }
   ]
}

– Deserialize with segment code:


ObjectMapper mapper = new ObjectMapper();
Object object = mapper.readValue(jsonString, valueType);

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:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

SourceCodes:
JacksonJsonIgnore
JacksonJsonView
JacksonJsonManagedBackReference
JacksonJsonJsonIdentityInfo



By grokonez | June 26, 2017.

Last updated on April 20, 2021.



Related Posts


8 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:

    {
       "@id":1,
       "id":1,
       "name":"Iphone 7",
       "company":{
          "@id":1,
          "id":1,
          "name":"Apple",
          "products":[
             1,
             {
                "@id":2,
                "id":2,
                "name":"IPadPro",
                "company":1
             },
             {
                "@id":3,
                "id":2,
                "name":"IPadPro",
                "company":1
             }
          ]
       }
    }
    

    But I should get like:

    {
       "@id":1,
       "id":1,
       "name":"Iphone 7",
       "company":{
          "@id":1,
          "id":1,
          "name":"Apple",
          "products":[
             {
                "@id":1,
                "id":1,
                "name":"Iphone 7",
                "company":1
             },
             {
                "@id":2,
                "id":2,
                "name":"IPadPro",
                "company":1
             },
             {
                "@id":3,
                "id":2,
                "name":"IPadPro",
                "company":1
             }
          ]
       }
    }
    

    How to achieve that ?

    1. Hello Thrinath,

      serialize Object To String with the info:

      {
         "@id":1,
         "id":1,
         "name":"Iphone 7",
         "company":{
            "@id":1,
            "id":1,
            "name":"Apple",
            "products":[
               1,
               {
                  "@id":2,
                  "id":2,
                  "name":"IPadPro",
                  "company":1
               },
               {
                  "@id":3,
                  "id":2,
                  "name":"IPadPro",
                  "company":1
               }
            ]
         }
      }
      

      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?

      {
         "@id":1,
         "id":1,
         "name":"Iphone 7",
         "company":{
            "@id":1,
            "id":1,
            "name":"Apple",
            "products":[
               {
                  "@id":1,
                  "id":1,
                  "name":"Iphone 7",
                  "company":1
               },
               {
                  "@id":2,
                  "id":2,
                  "name":"IPadPro",
                  "company":1
               },
               {
                  "@id":3,
                  "id":2,
                  "name":"IPadPro",
                  "company":1
               }
            ]
         }
      }
      

      Regards,
      JSA

  2. Wonderful solution. @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property=”@id”, scope = Product.class)

    Gracias!

  3. Hi Author,
    Thanks for the nice tutorials and great posts. However I found that @JsonManagedReference, @JsonBackReference does not gives you the data associated with @OneToMany and @ManyToOne scenario, also when using @JsonIgnoreProperties does skip associated entity data. How to solve this?

  4. I see this solution (@JsonIgnore, @JsonView, etc) provided everywhere.
    It seems to be an unsatisfactory solution if you actually need the values associated
    with the entity. Perhaps I’m not creative enough but in a RESTful (or otherwise)
    service I don’t see a way to send everything needed except by making two DB calls.
    Can you clarify a way out of doing that?

    Thanks.

Got Something To Say:

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

*