Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 39 – @JsonIgnore Annotation of Jackson API – Exclude Field of Pojo From Serialization and Deserialization

Posted on 04/23/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation called @JsonIgnore of Jackson library which helps in restricting properties of a POJO class from serialization and deserialization.

Since we are using Jackson API of Java for this example, make sure you have the latest dependency of Jackson Databind in your project classpath. I have used below Jackson dependency for this post:-

  com.fasterxml.jackson.core jackson-databind 2.11.1


There may be multiple fields in a Pojo class and sometimes you don’t require some fields to participate in serialization and deserialization process. I can give a scenario where we may need to ignore some properties of a Pojo class.

In a POJO class, we may have properties whose values are derived from other properties. For example- If we know the first name and last name of a person we can get their full name. If we know the age of a person we can get to know if a person is eligible for the vote or not. We can have getter methods for such properties to return values but should not be allowed to set from outside.

Consider below POJO.

package JacksonTutorials;

public class EmployeePojoWithoutJsonIgnore {

        // private variables or data members of pojo class
        private String firstName;
        private String lastName;
        private String gender;
        private int age;
        private double salary;
        private boolean married;
        
        private String fullName;
        private boolean eligibleForVote;
        
        // Getter and setter methods
        public String getFirstName() {
                return firstName;
        }
        
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
        public String getLastName() {
                return lastName;
        }
        public void setLastName(String lastName) {
                this.lastName = lastName;
        }
        public String getGender() {
                return gender;
        }
        public void setGender(String gender) {
                this.gender = gender;
        }
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
        public double getSalary() {
                return salary;
        }
        public void setSalary(double salary) {
                this.salary = salary;
        }
        public boolean getMarried() {
                return married;
        }
        public void setMarried(boolean married) {
                this.married = married;
        } 
        
        public String getFullName() {
                return this.fullName;
        }
        public boolean getEligibleForVote() {
                return this.eligibleForVote;
        }

        public void setFullName(String fullName) {
                this.fullName = fullName;
        }

        public void setEligibleForVote(boolean eligibleForVote) {
                this.eligibleForVote = eligibleForVote;
        }
        
        
}
@Test
        public void serializationWithoutJsonIgnore() throws JsonProcessingException {
                // Just create an object of Pojo class
                EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore = new EmployeePojoWithoutJsonIgnore();
                employeePojoWithoutJsonIgnore.setFirstName("Amod");
                employeePojoWithoutJsonIgnore.setLastName("Mahajan");
                employeePojoWithoutJsonIgnore.setAge(29);
                employeePojoWithoutJsonIgnore.setGender("Male");
                employeePojoWithoutJsonIgnore.setSalary(12323.56);
                employeePojoWithoutJsonIgnore.setMarried(false);
                employeePojoWithoutJsonIgnore.setFullName("Animesh Prashant");
                employeePojoWithoutJsonIgnore.setEligibleForVote(false);

                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithoutJsonIgnore);
                System.out.println("Serialization...");
                System.out.println(employeeJson);
        }
Serialization...
{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : "Male",
  "age" : 29,
  "salary" : 12323.56,
  "married" : false,
  "fullName" : "Animesh Prashant",
  "eligibleForVote" : false
}
@Test
        public void deserializationWithoutJsonIgnore() throws JsonMappingException, JsonProcessingException
        {
                String employeeString = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\",\r\n" + 
                                "  \"gender\" : \"Male\",\r\n" + 
                                "  \"age\" : 29,\r\n" + 
                                "  \"salary\" : 12323.56,\r\n" + 
                                "  \"married\" : false,\r\n" + 
                                "  \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + 
                                "  \"eligibleForVote\" : false\r\n" + 
                                "}";
                
                ObjectMapper objectMapper = new ObjectMapper();
                EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithoutJsonIgnore.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employeePojoWithoutJsonIgnore2.getFirstName());
                System.out.println("Last name :- "+employeePojoWithoutJsonIgnore2.getLastName());
                System.out.println("Age :- "+employeePojoWithoutJsonIgnore2.getAge());
                System.out.println("Gender :- "+employeePojoWithoutJsonIgnore2.getGender());
                System.out.println("Salary :- "+employeePojoWithoutJsonIgnore2.getSalary());
                System.out.println("Married :- "+employeePojoWithoutJsonIgnore2.getMarried());
                System.out.println("Eligible for vote :- "+employeePojoWithoutJsonIgnore2.getEligibleForVote());
                System.out.println("Full name :- "+employeePojoWithoutJsonIgnore2.getFullName());
        }
Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 29
Gender :- Male
Salary :- 12323.56
Married :- false
Eligible for vote :- false
Full name :- Amod Mahajan Gupta

If you observe output carefully we see values of properties of “fullName” and “eligibleForVote” have set wrongly. We should have calculated it. Ideally, these fields should have getter methods only just for quick access.

package JacksonTutorials;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerialiDeserialExampleWithoutJsonIgnore {

        @Test
        public void serializationWithoutJsonIgnore() throws JsonProcessingException {
                // Just create an object of Pojo class
                EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore = new EmployeePojoWithoutJsonIgnore();
                employeePojoWithoutJsonIgnore.setFirstName("Amod");
                employeePojoWithoutJsonIgnore.setLastName("Mahajan");
                employeePojoWithoutJsonIgnore.setAge(29);
                employeePojoWithoutJsonIgnore.setGender("Male");
                employeePojoWithoutJsonIgnore.setSalary(12323.56);
                employeePojoWithoutJsonIgnore.setMarried(false);
                employeePojoWithoutJsonIgnore.setFullName("Animesh Prashant");
                employeePojoWithoutJsonIgnore.setEligibleForVote(false);

                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithoutJsonIgnore);
                System.out.println("Serialization...");
                System.out.println(employeeJson);
        }
        
        @Test
        public void deserializationWithoutJsonIgnore() throws JsonMappingException, JsonProcessingException
        {
                String employeeString = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\",\r\n" + 
                                "  \"gender\" : \"Male\",\r\n" + 
                                "  \"age\" : 29,\r\n" + 
                                "  \"salary\" : 12323.56,\r\n" + 
                                "  \"married\" : false,\r\n" + 
                                "  \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + 
                                "  \"eligibleForVote\" : false\r\n" + 
                                "}";
                
                ObjectMapper objectMapper = new ObjectMapper();
                EmployeePojoWithoutJsonIgnore employeePojoWithoutJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithoutJsonIgnore.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employeePojoWithoutJsonIgnore2.getFirstName());
                System.out.println("Last name :- "+employeePojoWithoutJsonIgnore2.getLastName());
                System.out.println("Age :- "+employeePojoWithoutJsonIgnore2.getAge());
                System.out.println("Gender :- "+employeePojoWithoutJsonIgnore2.getGender());
                System.out.println("Salary :- "+employeePojoWithoutJsonIgnore2.getSalary());
                System.out.println("Married :- "+employeePojoWithoutJsonIgnore2.getMarried());
                System.out.println("Eligible for vote :- "+employeePojoWithoutJsonIgnore2.getEligibleForVote());
                System.out.println("Full name :- "+employeePojoWithoutJsonIgnore2.getFullName());
        }
}

There may be some fields that may be optional or just want to ignore it for time being.

If we want to ignore any property of a POJO class from serialization and deserialization we can use @JsonIgnore provided by Jackson API on that property. It is a marker annotation that indicates that the logical property that the accessor is to be ignored by introspection-based serialization and deserialization functionality. Annotation only needs to be added to one of the accessors (often getter method, but may be setter, field or creator parameter), if the complete removal of the property is desired.

package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class EmployeePojoWithJsonIgnore {

 // private variables or data members of pojo class
 private String firstName;
 private String lastName;
 private String gender;
 private int age;
 private double salary;
 private boolean married;
 
 @JsonIgnore
 private String fullName;
 @JsonIgnore
 private boolean eligibleForVote;
 
 // Getter and setter methods
 public String getFirstName() {
 return firstName;
 }
 
 public void setFirstName(String firstName) {
 this.firstName = firstName;
 }
 public String getLastName() {
 return lastName;
 }
 public void setLastName(String lastName) {
 this.lastName = lastName;
 }
 public String getGender() {
 return gender;
 }
 public void setGender(String gender) {
 this.gender = gender;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 public double getSalary() {
 return salary;
 }
 public void setSalary(double salary) {
 this.salary = salary;
 }
 public boolean getMarried() {
 return married;
 }
 public void setMarried(boolean married) {
 this.married = married;
 } 
 
 public String getFullName() {
 return this.fullName;
 }
 public boolean getEligibleForVote() {
 return this.eligibleForVote;
 }

 public void setFullName(String fullName) {
 this.fullName = fullName;
 }

 public void setEligibleForVote(boolean eligibleForVote) {
 this.eligibleForVote = eligibleForVote;
 }
}
@Test
        public void serializationWithJsonIgnore() throws JsonProcessingException {
                // Just create an object of Pojo class
                EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore = new EmployeePojoWithJsonIgnore();
                employeePojoWithJsonIgnore.setFirstName("Amod");
                employeePojoWithJsonIgnore.setLastName("Mahajan");
                employeePojoWithJsonIgnore.setAge(29);
                employeePojoWithJsonIgnore.setGender("Male");
                employeePojoWithJsonIgnore.setSalary(12323.56);
                employeePojoWithJsonIgnore.setMarried(false);
                employeePojoWithJsonIgnore.setFullName("Animesh Prashant");
                employeePojoWithJsonIgnore.setEligibleForVote(false);

                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnore);
                System.out.println("Serialization...");
                System.out.println(employeeJson);
                
        }
Serialization...
{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : "Male",
  "age" : 29,
  "salary" : 12323.56,
  "married" : false
}
@Test
        public void deserializationWithJsonIgnore() throws JsonMappingException, JsonProcessingException
        {
                String employeeString = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\",\r\n" + 
                                "  \"gender\" : \"Male\",\r\n" + 
                                "  \"age\" : 29,\r\n" + 
                                "  \"salary\" : 12323.56,\r\n" + 
                                "  \"married\" : false,\r\n" + 
                                "  \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + 
                                "  \"eligibleForVote\" : false\r\n" + 
                                "}";
                
                ObjectMapper objectMapper = new ObjectMapper();
                EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnore.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employeePojoWithJsonIgnore2.getFirstName());
                System.out.println("Last name :- "+employeePojoWithJsonIgnore2.getLastName());
                System.out.println("Age :- "+employeePojoWithJsonIgnore2.getAge());
                System.out.println("Gender :- "+employeePojoWithJsonIgnore2.getGender());
                System.out.println("Salary :- "+employeePojoWithJsonIgnore2.getSalary());
                System.out.println("Married :- "+employeePojoWithJsonIgnore2.getMarried());
                System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnore2.getEligibleForVote());
                System.out.println("Full name :- "+employeePojoWithJsonIgnore2.getFullName());
        }
Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 29
Gender :- Male
Salary :- 12323.56
Married :- false
Eligible for vote :- false
Full name :- null
package JacksonTutorials;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerialiDeserialExampleWithJsonIgnore {

        @Test
        public void serializationWithJsonIgnore() throws JsonProcessingException {
                // Just create an object of Pojo class
                EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore = new EmployeePojoWithJsonIgnore();
                employeePojoWithJsonIgnore.setFirstName("Amod");
                employeePojoWithJsonIgnore.setLastName("Mahajan");
                employeePojoWithJsonIgnore.setAge(29);
                employeePojoWithJsonIgnore.setGender("Male");
                employeePojoWithJsonIgnore.setSalary(12323.56);
                employeePojoWithJsonIgnore.setMarried(false);
                employeePojoWithJsonIgnore.setFullName("Animesh Prashant");
                employeePojoWithJsonIgnore.setEligibleForVote(false);

                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnore);
                System.out.println("Serialization...");
                System.out.println(employeeJson);
                
        }
        
        @Test
        public void deserializationWithJsonIgnore() throws JsonMappingException, JsonProcessingException
        {
                String employeeString = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\",\r\n" + 
                                "  \"gender\" : \"Male\",\r\n" + 
                                "  \"age\" : 29,\r\n" + 
                                "  \"salary\" : 12323.56,\r\n" + 
                                "  \"married\" : false,\r\n" + 
                                "  \"fullName\" : \"Amod Mahajan Gupta\",\r\n" + 
                                "  \"eligibleForVote\" : false\r\n" + 
                                "}";
                
                ObjectMapper objectMapper = new ObjectMapper();
                EmployeePojoWithJsonIgnore employeePojoWithJsonIgnore2 = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnore.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employeePojoWithJsonIgnore2.getFirstName());
                System.out.println("Last name :- "+employeePojoWithJsonIgnore2.getLastName());
                System.out.println("Age :- "+employeePojoWithJsonIgnore2.getAge());
                System.out.println("Gender :- "+employeePojoWithJsonIgnore2.getGender());
                System.out.println("Salary :- "+employeePojoWithJsonIgnore2.getSalary());
                System.out.println("Married :- "+employeePojoWithJsonIgnore2.getMarried());
                System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnore2.getEligibleForVote());
                System.out.println("Full name :- "+employeePojoWithJsonIgnore2.getFullName());
        }
}

We have values for fields fullName and eligibleForVote in Json but it has not been deserialized as you can see it has default values not from Json.

@JsonIgnore annotation can be used with getter and setter methods as well.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: How To Remove Duplicate Values From List Using Java Stream API?
Next Post: REST Assured Tutorial 30 – How To Create POJO Classes Of A JSON Array Payload

Related Posts

TestNG Tutorials 9: Internal Logic Of Generation Of TestNG.xml Uncategorized
March 30, 2018 – Make Selenium Easy Uncategorized
JSON with JSONPath Uncategorized
staleExc – Make Selenium Easy Uncategorized
Make Selenium Easy – Page 46 of 47 – Uncategorized
nodejs Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark