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

73 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.

  5. Does your blog have a contact page? I’m having trouble locating it but, I’d like to send you an e-mail.
    I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it
    grow over time.

  6. This design is spectacular! You definitely know how
    to keep a reader entertained. Between your wit
    and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job.
    I really loved what you had to say, and more than that,
    how you presented it. Too cool!

  7. Heya are using WordPress for your site platform? I’m new to
    the blog world but I’m trying to get started and create my own. Do you require any html coding expertise to make your own blog?
    Any help would be greatly appreciated!

  8. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored material stylish.

    nonetheless, you command get got an shakiness over that you wish be delivering the following.
    unwell unquestionably come more formerly again since exactly
    the same nearly very often inside case you shield this increase.

  9. Wonderful blog! Do you have any tips for aspiring
    writers? I’m planning to start my own site soon but I’m a little lost on everything.

    Would you propose starting with a free platform
    like WordPress or go for a paid option? There are so many choices out there that I’m totally confused
    .. Any ideas? Kudos!

  10. Thank you for another informative site. The place else may I get that kind of info written in such
    a perfect method? I have a project that I am simply
    now running on, and I have been on the glance out for such info.

  11. After I originally left a comment I appear to
    have clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I recieve 4 emails with
    the same comment. Is there a way you are able to remove me from that service?

    Cheers!

  12. you’re really a good webmaster. The web site loading pace is incredible.
    It kind of feels that you’re doing any unique trick. Moreover, The contents
    are masterwork. you’ve performed a excellent job on this
    matter!

  13. I’m not that much of a online reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come back in the future.
    All the best

  14. Great post. I was checking continuously this blog and I
    am impressed! Very helpful information specially the last
    part 🙂 I care for such information much. I was looking for this certain info for a very long time.
    Thank you and best of luck.

  15. Hi, I do think this is an excellent web site. I stumbledupon it 😉 I may come back once
    again since I book-marked it. Money and freedom is the greatest
    way to change, may you be rich and continue to guide other people.

  16. We stumbled over here different website and thought I might as well check things out.
    I like what I see so now i am following you. Look forward to looking into your web page yet
    again.

  17. Howdy! I could have sworn I’ve been to this blog
    before but after browsing through some of the post I realized
    it’s new to me. Nonetheless, I’m definitely happy I found
    it and I’ll be bookmarking and checking back often!

  18. Hey! Would you mind if I share your blog with my myspace
    group? There’s a lot of people that I think would really enjoy
    your content. Please let me know. Cheers

  19. You actually make it seem so easy with your presentation but I find this matter to
    be really something which I think I would never understand.
    It seems too complex and very broad for me.
    I’m looking forward for your next post, I’ll try to get the hang of it!

  20. Someone necessarily help to make significantly articles I would state.
    This is the very first time I frequented your web page and thus far?

    I surprised with the research you made to
    make this actual post incredible. Wonderful job!

  21. Howdy! This blog post couldn’t be written any better! Looking at this article reminds
    me of my previous roommate! He continually kept talking about this.

    I’ll send this information to him. Fairly certain he will have a great read.

    Many thanks for sharing!

  22. Hey there would you mind letting me know which webhost you’re utilizing?

    I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads
    a lot faster then most. Can you recommend a good
    hosting provider at a reasonable price? Kudos, I appreciate it!

  23. Greate post. Keep writing such kind of info on your page.
    Im really impressed by your site.
    Hey there, You have performed an incredible job. I will definitely digg it and
    in my view recommend to my friends. I am sure they’ll
    be benefited from this web site.

  24. Excellent goods from you, man. I have understand your stuff
    previous to and you are just too fantastic.
    I really like what you have acquired here,
    certainly like what you’re saying and the way in which you say
    it. You make it entertaining and you still take care of to
    keep it smart. I can not wait to read far more from you.

    This is really a terrific website.

  25. Greetings! This is my 1st comment here so I just wanted to give a quick shout out and
    say I genuinely enjoy reading your articles. Can you suggest any other blogs/websites/forums that go over the same topics?

    Appreciate it!

  26. I’m extremely inspired together with your writing talents as neatly as
    with the format in your weblog. Is this a paid theme or did you customize it yourself?
    Anyway keep up the excellent high quality writing, it is uncommon to
    peer a nice blog like this one these days..

  27. Hello very cool website!! Man .. Excellent ..
    Wonderful .. I’ll bookmark your website and take the feeds additionally?
    I’m satisfied to seek out a lot of helpful info right
    here within the publish, we’d like work out extra techniques on this regard,
    thank you for sharing. . . . . .

  28. Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point. You definitely know what youre talking
    about, why waste your intelligence on just posting videos to your weblog when you could be giving us something enlightening to
    read?

  29. Howdy exceptional blog! Does running a blog like this require a large amount of
    work? I have no understanding of coding but I was hoping to start my own blog soon. Anyhow,
    if you have any recommendations or tips for new blog owners please share.

    I understand this is off topic but I just had to
    ask. Kudos!

  30. My brother suggested I might like this blog. He was totally right.
    This post truly made my day. You can not imagine simply how much time I
    had spent for this information! Thanks!

  31. I love your blog.. very nice colors & theme.
    Did you make this website yourself or did you hire someone to do it for
    you? Plz answer back as I’m looking to design my own blog
    and would like to find out where u got this from.
    appreciate it

  32. What i don’t realize is in fact how you are not actually a
    lot more smartly-favored than you might be now. You’re so intelligent.
    You realize therefore significantly in relation to this subject, made me for my part believe it from
    a lot of numerous angles. Its like women and men are not interested unless it is one thing to accomplish with Woman gaga!

    Your individual stuffs nice. Always take care of it up!

  33. Hi exceptional website! Does running a blog such as
    this require a large amount of work? I have absolutely no
    expertise in coding but I was hoping to start my own blog in the near future.
    Anyhow, should you have any suggestions or tips for new blog owners please share.

    I know this is off topic however I just had to ask.
    Thank you!

  34. I’ve been browsing online more than 4 hours today, yet I never found any interesting article like yours.

    It’s pretty worth enough for me. In my view, if all website owners and
    bloggers made good content as you did, the web will be a lot more useful than ever before.

  35. Great post. I was checking continuously this blog and I’m
    impressed! Very helpful info specifically the last
    part 🙂 I care for such information much. I was seeking this particular info for a long
    time. Thank you and best of luck.

  36. Hi there would you mind letting me know which webhost you’re utilizing?

    I’ve loaded your blog in 3 completely different web browsers and I must
    say this blog loads a lot faster then most. Can you recommend a good hosting provider
    at a reasonable price? Many thanks, I appreciate it!

  37. Undeniably believe that which you said. Your favorite
    justification seemed to be on the internet the easiest thing to be aware of.

    I say to you, I definitely get irked while people think about worries that they just do not know about.
    You managed to hit the nail upon the top as well as defined
    out the whole thing without having side effect , people can take a signal.
    Will probably be back to get more. Thanks

  38. Just desire to say your article is as astounding.
    The clearness in your post is just great and i can assume you are an expert on this subject.

    Well with your permission let me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please carry on the enjoyable work.

  39. Nice post. I learn something new and challenging on websites I stumbleupon on a daily basis. It’s always useful to read through content from other authors and practice a little something from other websites. |

Leave a Reply

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