🎓 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
Jackson is a popular Java library used for converting Java objects into JSON format and vice versa. A common scenario is the need to customize the naming of fields during the serialization to or deserialization from JSON. Jackson’s @JsonProperty annotation addresses this need.
@JsonProperty Annotation Overview
The @JsonProperty annotation signifies the desired property name in the JSON content. It's instrumental when you want to rename the field in the JSON output, especially when the Java field name and the JSON property name aren't identical.
2. Development Steps
1. Create a new Maven project.
2. Add Jackson dependencies.
3. Design an Employee class using @JsonProperty.
4. Create a separate class for the serialization logic.
5. Create a main class to demonstrate the serialization.
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
Let’s structure our program using the Employee Management System example:
// Employee.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class Employee {
@JsonProperty("full_name")
private String name;
@JsonProperty("years_of_experience")
private int experience;
// Standard constructor, getters, and setters...
}
// JsonSerializer.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonSerializer {
public static String serializeToJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(object);
}
}
// MainClass.java
public class MainClass {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setName("John Doe");
emp.setExperience(5);
try {
String json = JsonSerializer.serializeToJson(emp);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
{"full_name":"John Doe","years_of_experience":5}
Code Explanation:
The Employee class defines the data structure and uses @JsonProperty to customize JSON property names.
The JsonSerializer class is responsible for serialization.
The MainClass instantiates an Employee object, serializes it using JsonSerializer, and prints the resulting JSON.
6. Conclusion
The @JsonProperty annotation in Jackson enables Java developers to tailor JSON property names during serialization/deserialization. This ensures that the JSON output can match specific standards or naming conventions, granting developers precise control over their JSON output.
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