🎓 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 widely-used Java library for JSON processing, facilitating both serialization and deserialization of Java objects to and from JSON. Occasionally, developers may find the need to exclude an entire type (class) from being serialized or deserialized. Jackson provides the @JsonIgnoreType annotation for this purpose. In this blog post, we will delve into its usage in the context of a User Management System.
Check out all the Java Jackson JSON tutorials and examples: 50+ Java Jackson JSON Tutorials with Examples
@JsonIgnoreType Annotation Overview
The @JsonIgnoreType annotation is used to mark a type (i.e., class or interface) to be ignored during serialization and deserialization processes. When a type is annotated with @JsonIgnoreType, any property with that type will be ignored.
2. Development Steps
1. Set up a new Maven project.
2. Incorporate the required Jackson dependencies.
3. Define classes, ensuring one class uses the @JsonIgnoreType annotation.
4. Craft a demonstration main class.
5. Serialize a sample object into JSON format.
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
// Address.java
@JsonIgnoreType
public class Address {
private String street;
private String city;
// Constructors, getters, setters...
}
// User.java
public class User {
private int id;
private String username;
private Address address;
// Constructors, getters, setters...
}
// MainDemo.java
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainDemo {
public static void main(String[] args) {
Address address = new Address("123 Elm Street", "Springfield");
User user = new User(1, "johndoe", address);
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(user);
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
{"id":1,"username":"johndoe"}
Code Explanation:
The Address class is marked with the @JsonIgnoreType annotation. Consequently, during serialization, any property of this type will be ignored by Jackson.
In our MainDemo class, even though the User object has an Address instance, it gets excluded during serialization due to the @JsonIgnoreType annotation. The resultant JSON output only contains the id and username fields of the User object, excluding the entire address field.
6. Conclusion
The @JsonIgnoreType annotation in Jackson offers developers a powerful tool for excluding entire types during the JSON serialization and deserialization processes. This can be particularly handy in situations where certain types should not be exposed in serialized representations or need to be excluded for other reasons.
Check out all the Java Jackson JSON tutorials and examples: 50+ Java Jackson JSON Tutorials with Examples
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