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

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

WBOY
Release: 2023-09-05 22:33:06
forward
1110 people have browsed it

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

@JsonManagedReference and @JsonBackReference annotations can be used to create JSON structures in > Two-way. The @JsonManagedReference annotation is a forward reference that is included during serialization, while the @JsonBackReference annotation is a backreference that is included in the sequence omitted during the transformation process.

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

Example

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);
      testBean.addEmployees(bean);
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
      System.out.println(jsonString);
   }
}
class ManagedReferenceBeanTest {
   public int empId = 115;
   public String empName = "Raja Ramesh";
   @JsonManagedReference
   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";
   @JsonBackReference
   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) {
      list.add(managedReferenceBeanTest);
   }
}
Copy after login

Output

{
   "empId" : 135,
   "empName" : "Adithya Ram",
   "manager" : {
      "empId" : 110,
      "empName" : "Sai Chaitanya"
   }
}
Copy after login

The above is the detailed content of When to use @JsonManagedReference and @JsonBackReference annotations in Java using Jackson?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template