🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Overview
When working with Jackson for JSON serialization/deserialization, developers often face challenges when dealing with bidirectional relationships in object models, such as the relationship between parents and their children. Infinite recursion is a common issue in this scenario. Jackson provides annotations like @JsonManagedReference and @JsonBackReference to handle these cases efficiently. In this article, we'll examine the @JsonManagedReference annotation, with a User Management System as an example.
Check out all the Java Jackson JSON tutorials and examples: 50+ Java Jackson JSON Tutorials with Examples
@JsonManagedReference Annotation Overview
The @JsonManagedReference annotation is used to annotate the property that manages the relationship. It helps Jackson navigate these relationships without falling into infinite loops. The other side of the relation would be annotated with @JsonBackReference.
2. Development Steps
1. Set up a new Maven project.
2. Add the necessary Jackson dependencies.
3. Define the entity classes, one of which utilizes the @JsonManagedReference annotation.
4. Create a main class to demonstrate serialization.
5. Serialize an example object to JSON.
3. Create a Maven Project
There are different ways to create a simple Maven project:
Create a Simple Maven Project using the Command Line Interface
Create a Simple Maven Project using Eclipse IDE
Create a Simple Maven Project using IntelliJ IDEA
4. Maven Dependencies
Open the pom.xml file, and add the following Jackson data binding dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
5. Code Program
// Address.java
public class Address {
private String city;
private String country;
@JsonBackReference
private User user;
// Constructors, getters, setters...
}
// User.java
public class User {
private int id;
private String name;
@JsonManagedReference
private Address address;
// Constructors, getters, setters...
}
// MainDemo.java
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainDemo {
public static void main(String[] args) {
Address address = new Address("New York", "USA");
User user = new User(1, "Alice", address);
address.setUser(user);
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(user);
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
{"id":1,"name":"Alice","address":{"city":"New York","country":"USA"}}
Code Explanation:
In our model, a User has an Address, and conversely, an Address references a User. This creates a bidirectional relationship.
The @JsonManagedReference annotation on the address property in the User class indicates that this field should be serialized, while the corresponding backreference @JsonBackReference in the Address class indicates that the user field should not be serialized to prevent infinite recursion.
When serializing the User object in our main demonstration, Jackson serializes the address property but avoids serializing the back reference to user in the Address class, thus eliminating any infinite loop scenario.
6. Conclusion
Jackson's @JsonManagedReference and @JsonBackReference annotations are invaluable tools for managing and navigating bidirectional relationships in Java entities during JSON serialization/deserialization. They prevent infinite recursion issues and ensure efficient data representation. When used wisely, they can handle complex object relationships seamlessly.
Check out all the Java Jackson JSON tutorials and examples: 50+ Java Jackson JSON Tutorials with Examples
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment