🎓 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 prominent Java library used for serializing Java objects to JSON and vice versa. When there's a need to dictate how an object should be constructed during deserialization, Jackson's @JsonCreator annotation comes into play.
@JsonCreator Annotation Overview
The @JsonCreator annotation is used to specify a method or constructor that should be employed by Jackson to create an instance of the class. This is especially useful when dealing with complex objects or when a class does not have a default constructor.
2. Development Steps
1. Begin by initiating a new Maven project.
2. Incorporate the required Jackson dependencies.
3. Define the User class with the @JsonCreator annotation.
4. Establish a separate class for the serialization and deserialization mechanism.
5. Construct a main class to showcase serialization and deserialization.
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 take a closer look at the User Management System:
// User.java
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
private final String id;
private final String name;
@JsonCreator
public User(@JsonProperty("id") String id, @JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
// Getters...
}
// UserSerializer.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class UserSerializer {
public static String serialize(User user) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(user);
}
public static User deserialize(String json) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, User.class);
}
}
// MainClass.java
public class MainClass {
public static void main(String[] args) {
User newUser = new User("001", "Alice");
try {
String json = UserSerializer.serialize(newUser);
System.out.println("Serialized JSON: " + json);
User deserialized = UserSerializer.deserialize(json);
System.out.println("Deserialized User Name: " + deserialized.getName());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
Serialized JSON: {"id":"001","name":"Alice"}
Deserialized User Name: Alice
Code Explanation:
The User class has a constructor annotated with @JsonCreator, which indicates to Jackson that this constructor should be used for deserialization.
The @JsonProperty annotations on the constructor parameters tell Jackson which JSON properties to bind to the constructor parameters.
Our UserSerializer class offers methods for serialization and deserialization, harnessing Jackson's ObjectMapper.
The MainClass demonstrates this by serializing and then deserializing a User object.
6. Conclusion
With the @JsonCreator annotation in Jackson, developers are endowed with enhanced control over object creation during deserialization. By specifying constructors or factory methods, one ensures the proper and desired object creation, thereby fostering robust and maintainable applications.
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