Close

Jackson JSON - Using @JsonManagedReference and @JsonBackReference for circular references

[Updated: Sep 18, 2018, Created: Sep 18, 2018]

@JsonManagedReference and @JsonBackReference are used to handle circular references.

@JsonManagedReference is used on a child reference of the target POJO.

@JsonBackReference is used in the corresponding child class. It is placed on the back-reference property.

Example

POJOs

public class Customer {
  private int id;
  private String name;
  @JsonManagedReference
  private Order order;
    .............
}
public class Order {
  private int orderId;
  private List<Integer> itemIds;
  @JsonBackReference
  private Customer customer;
    .............
}

Serializing and deserializing

public class ExampleMain {
  public static void main(String[] args) throws IOException {
      Order order = new Order();
      order.setOrderId(1);
      order.setItemIds(List.of(10, 30));

      Customer customer = new Customer();
      customer.setId(2);
      customer.setName("Peter");
      customer.setOrder(order);
      order.setCustomer(customer);

      System.out.println(customer);
      System.out.println("-- serializing --");
      ObjectMapper om = new ObjectMapper();
      String s = om.writeValueAsString(customer);
      System.out.println(s);
      System.out.println("-- deserializing --");
      Customer customer2 = om.readValue(s, Customer.class);
      System.out.println(customer2);
  }
}
Customer{id=2, name='Peter', order=Order{id=1, itemIds=[10, 30]}}
-- serializing --
{"id":2,"name":"Peter","order":{"orderId":1,"itemIds":[10,30]}}
-- deserializing --
Customer{id=2, name='Peter', order=Order{id=1, itemIds=[10, 30]}}

Collections involving circular reference

POJOs

public class Dept {
  private int deptId;
  @JsonManagedReference
  List<Employee> employees;
  private String name;
    .............
}
public class Employee {
  private int empId;
  @JsonBackReference
  private Dept dept;
  private String name;
    .............
}

Serializing/Deserializing

public class ExampleMain2 {

  public static void main(String[] args) throws IOException {
      Employee emp1 = new Employee();
      emp1.setEmpId(1);
      emp1.setName("Tina");

      Employee emp2 = new Employee();
      emp2.setEmpId(2);
      emp2.setName("Joe");

      Dept dept = new Dept();
      dept.setName("IT");
      dept.setDeptId(3);
      dept.setEmployees(List.of(emp1, emp2));

      System.out.println("-- before serializing dept --");
      System.out.println(dept);

      System.out.println("-- json string after serializing --");
      ObjectMapper om = new ObjectMapper();
      String s = om.writeValueAsString(dept);
      System.out.println(s);
      System.out.println("-- deserializing --");
      Dept dept1 = om.readValue(s, Dept.class);
      System.out.println(dept1);
  }
}
-- before serializing dept --
Dept{deptId=3, employees=[Employee{empId=1, deptId=3, name='Tina'}, Employee{empId=2, deptId=3, name='Joe'}], name='IT'}
-- json string after serializing --
{"deptId":3,"employees":[{"empId":1,"name":"Tina"},{"empId":2,"name":"Joe"}],"name":"IT"}
-- deserializing --
Dept{deptId=3, employees=[Employee{empId=1, deptId=3, name='Tina'}, Employee{empId=2, deptId=3, name='Joe'}], name='IT'}

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.6: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 10
  • Maven 3.5.4

@JsonManagedReference and @JsonBackReference Example Select All Download
  • jackson-json-managed-reference
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Customer.java

    See Also