🎓 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 library for Java that allows for easy serialization and deserialization of Java objects to and from JSON. One of the annotations provided by the Jackson Library is @JsonIgnore. This annotation is used to ignore certain properties during serialization and deserialization processes. This is especially useful when you want to hide or omit sensitive or unnecessary data when converting between JSON and Java objects.
Jackson @JsonIgnore Annotation Overview
The @JsonIgnore annotation is placed above a field or getter method of a Java object to indicate that this particular property should not be considered during serialization or deserialization. This ensures that the specified field is neither included in the generated JSON output nor populated from an incoming JSON during deserialization.
2. Development Steps
1. Kickstart a new Maven project.
2. Integrate the essential Jackson dependencies.
3. Design the Employee class and apply the @JsonIgnore annotation to specific fields.
4. Develop a separate class responsible for the serialization process.
5. Establish a primary class to illustrate 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
// Employee.java
import com.fasterxml.jackson.annotation.JsonIgnore;
public class Employee {
private String name;
private int age;
// Ignoring this field during serialization
@JsonIgnore
private String ssn;
// Standard constructors, 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("Jane Smith");
emp.setAge(30);
emp.setSsn("123-45-6789");
try {
String json = JsonSerializer.serializeToJson(emp);
System.out.println(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
{"name":"Jane Smith","age":30}
Code Explanation:
In the Employee class, we have an ssn (Social Security Number) field annotated with @JsonIgnore. This indicates to Jackson that when the Employee object is serialized, the ssn field should be omitted.
The JsonSerializer class is a utility class responsible for the serialization, using Jackson's ObjectMapper.
The MainClass brings everything together: it creates an Employee instance, triggers the serialization through JsonSerializer, and then prints the resulting JSON.
6. Conclusion
The @JsonIgnore annotation in Jackson provides a straightforward approach for developers to exercise control over what gets included in the JSON output. This offers a balance between data transparency and privacy, making sure only the necessary data is exposed.
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