When to use @JsonManagedReference and @JsonBackReference annotations using Jackson in Java?

JavaJSONObject Oriented ProgrammingProgramming

The @JsonManagedReference and @JsonBackReference annotations can be used to create a JSON structure in a bidirectional way. The @JsonManagedReference annotation is a forward reference that includes during the serialization process whereas @JsonBackReference annotation is a backreference that omits during the serialization process.

In the below example, we can implement @JsonManagedReference and @JsonBackReference annotations.


import java.util.*;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
public class ManagedReferenceBackReferenceTest {
   public static void main(String args[]) throws JsonProcessingException {
      BackReferenceBeanTest testBean = new BackReferenceBeanTest(110, "Sai Chaitanya");
      ManagedReferenceBeanTest bean = new ManagedReferenceBeanTest(135, "Adithya Ram", testBean);
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
class ManagedReferenceBeanTest {
   public int empId = 115;
   public String empName = "Raja Ramesh";
   public BackReferenceBeanTest manager;
   public ManagedReferenceBeanTest(int empId, String empName, BackReferenceBeanTest manager) {
      this.empId = empId;
      this.empName = empName;
      this.manager = manager;
class BackReferenceBeanTest {
   public int empId = 125;
   public String empName = "Jai Dev";
   public List<ManagedReferenceBeanTest> list;
   public BackReferenceBeanTest(int empId, String empName) {
      this.empId = empId;
      this.empName = empName;
      list = new ArrayList<ManagedReferenceBeanTest>();
   public void addEmployees(ManagedReferenceBeanTest managedReferenceBeanTest) {


   "empId" : 135,
   "empName" : "Adithya Ram",
   "manager" : {
      "empId" : 110,
      "empName" : "Sai Chaitanya"
Updated on 08-Jul-2020 11:36:22