🎓 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
The Jackson library in Java provides a myriad of annotations to control the serialization and deserialization of Java objects to and from JSON format. One such annotation, which comes in handy when handling multiple possible names for a property during deserialization, is @JsonAlias. In this post, we will explore its use within the context of an Employee Management System.
@JsonAlias Annotation Overview
Introduced in Jackson 2.9, the @JsonAlias annotation provides an alternative means to define one or more alternative names for a property during the deserialization process. This is especially useful when the incoming JSON might have different field names for the same property.
2. Development Steps
1. Set up a new Maven project.
2. Incorporate the essential Jackson dependencies.
3. Craft an Employee class utilizing the @JsonAlias annotation.
4. Develop a main class for demonstration and testing purposes.
5. Deserialize sample JSON data into the Employee object.
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
Below are the classes for our Employee Management System:
// Employee.java
public class Employee {
private int id;
@JsonAlias({ "fullName", "full_name" })
private String name;
// Standard getters and setters...
}
// MainDemo.java
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainDemo {
public static void main(String[] args) {
// JSON with 'fullName'
String json1 = "{\"id\": 1, \"fullName\": \"John Doe\"}";
// JSON with 'full_name'
String json2 = "{\"id\": 2, \"full_name\": \"Jane Smith\"}";
ObjectMapper mapper = new ObjectMapper();
try {
Employee employee1 = mapper.readValue(json1, Employee.class);
System.out.println("Employee ID: " + employee1.getId() + ", Name: " + employee1.getName());
Employee employee2 = mapper.readValue(json2, Employee.class);
System.out.println("Employee ID: " + employee2.getId() + ", Name: " + employee2.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Employee ID: 1, Name: John Doe Employee ID: 2, Name: Jane Smith
Code Explanation:
In the Employee class, the name field is annotated with @JsonAlias, accepting both "fullName" and "full_name" as valid JSON field names. This means that while deserializing if either of these names is encountered, they will map to the name field in the Employee class.
The MainDemo class further exemplifies this by successfully deserializing two different JSON strings with different name attributes.
6. Conclusion
The @JsonAlias annotation in Jackson offers an elegant solution for scenarios where incoming JSON might have varying field names for the same Java property. It ensures flexibility and robustness in deserialization processes, making the handling of diverse data sources more straightforward and efficient.
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