JSON Serialization errors in Jackson lib

Asha Somayajula
Javascribe
Published in
2 min readNov 25, 2021

--

One of the simple use cases for mappings between entity objects is is One-to-many, uni directional, or bi directional. The relationship is very easily expressed as

One Order has multiple items or products to sell.

public class Order {
public int id;
public String name;
public List<Products> products;
}

public class Product {
public int id;
public String name;
public int qty;
public int price;
}

That’s easy enough. Now with the entity-relationship definition added.

@Entity
@Table (name= “order”)
public class Order {
public int id;
public String name;

@OneToMany(mappedBy=”order”)
public List<Products> products;
}

@Entity
@Table (name= “product”)
public class Product {
public int id;
public String name;
public int qty;
public int price;

@JoinColumn(name=”order_id”, nullable=false)
public Order order;
}

Once the relationship definition is in place, the translation and eager or lazy fetch of the collections is straightforward. yet, one thing remains the serialization and JSON translation of the payload. That should be as straight forward you would think. Wrong, if translated as is, you would run into this error pretty quick.

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain:

--

--