🎓 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 offers a powerful suite of annotations to customize the way Java objects are serialized into JSON and vice versa. One of the most versatile annotations is @JsonSerialize, which is employed to specify a custom serializer for an object. In this tutorial, we'll delve into the utilization of this annotation using an Employee Management System as our example.
@JsonSerialize Annotation Overview
The @JsonSerialize annotation in Jackson is employed to define a custom serializer for a Java object or property. It grants developers increased control over the JSON output, allowing for more tailored serialization than what's provided out-of-the-box by Jackson.
2. Development Steps
1. Establish a new Maven project.
2. Add the requisite Jackson dependencies.
3. Construct an Employee class, employing the @JsonSerialize annotation.
4. Define a custom serializer class.
5. Create a demonstration class with the main method.
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
For our Employee Management System, consider the subsequent classes:
// Employee.java
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
public class Employee {
private int empId;
private String empName;
private double salary;
@JsonSerialize(using = SalarySerializer.class)
public double getSalary() {
return salary;
}
public Employee(int empId, String empName, double salary) {
this.empId = empId;
this.empName = empName;
this.salary = salary;
}
public int getEmpId() {
return empId;
}
public String getEmpName() {
return empName;
}
}
// SalarySerializer.java
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
public class SalarySerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// Display salary rounded to two decimal places
gen.writeString(String.format("%.2f", value));
}
}
// MainClass.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainClass {
public static void main(String[] args) {
Employee employee = new Employee(1, "Alice", 5000.4567);
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(employee);
System.out.println("Serialized JSON: " + json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
Serialized JSON: {"empId":1,"empName":"Alice","salary":"5000.46"}
Code Explanation:
In the Employee class, the salary property uses the @JsonSerialize annotation to specify SalarySerializer as its serializer. This custom serializer, SalarySerializer, modifies the serialization of the salary property to round to two decimal places. The MainClass shows
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