🎓 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 an essential tool in the Java ecosystem, providing capabilities to serialize Java objects into JSON and vice versa. One of its features is the @JsonGetter annotation, which customizes the output name of a getter method during serialization.
Annotation Overview
The @JsonGetter annotation in Jackson is employed to signify a method as a getter method for a specific property. Additionally, you can specify a custom name for the property in the serialized JSON, which may differ from the actual Java property name.
2. Development Steps
1. Establish a new Maven project.
2. Incorporate the Jackson dependencies.
3. Construct the Student class, applying the @JsonGetter annotation.
4. Develop a class for serialization.
5. Implement a main method for demonstration.
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
Within our Student Management System:
// Student.java
import com.fasterxml.jackson.annotation.JsonGetter;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@JsonGetter("studentName")
public String retrieveName() {
return name;
}
public int getAge() {
return age;
}
}
// StudentSerializer.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class StudentSerializer {
public static String serialize(Student student) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(student);
}
}
// MainClass.java
public class MainClass {
public static void main(String[] args) {
Student student = new Student("John Doe", 20);
try {
String json = StudentSerializer.serialize(student);
System.out.println("Serialized JSON: " + json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
Serialized JSON: {"studentName":"John Doe","age":20}
Code Explanation:
The Student class includes a custom getter method named retrieveName().
By using the @JsonGetter annotation and specifying "studentName" as its value, the serialized output will have "studentName" as the property name instead of a default derived from the getter method name. The StudentSerializer class manages the object-to-JSON conversion.
In MainClass, we instantiate a student object and serialize it. The resulting JSON confirms that the custom name provided by @JsonGetter is utilized.
6. Conclusion
Jackson's @JsonGetter offers developers a straightforward method to customize the naming of properties in the serialized JSON. This flexibility ensures that the output JSON can be tailored to adhere to specific naming conventions or requirements, irrespective of the method names in the Java class.
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