Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 40 – @JsonIgnoreProperties Annotation Of Jackson API – Exclude Field Of Pojo From Serialization Or Deserialization or Both

Posted on 03/21/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation called @JsonIgnoreProperties of Jackson library which helps in restricting properties of a POJO class from serialization or deserialization or both at the class level or property level.

You must refer below these prerequisite posts to understand this concept better:-

How Getter & Setter Methods Matter For Serialization Deserialization Using POJO

@JsonIgnore Annotation Of Jackson API – Exclude Field Of Pojo 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

We use @JsonIgnore annotation at an individual property level or its one of the accessors to prevent a particular field from serialization and deserialization process. It has some limitations as below:-

  1. To ignore multiple properties, you need to mark each property with @JsonIgnore that makes it difficult to manage.
  2. If you mention @JsonIgnore at any one place i.e. at a property or its one of accessors then that property will be ignored from serialization and deserialization. We can not control if we want a property to be ignored by serialization or deserialization only.

These limitations can be overcome using @JsonIgnoreProperties.

It is an annotation that can be used to either suppress serialization of properties (during serialization) or ignore the processing of JSON properties read (during deserialization). This can be used at class level as well as at an individual property level like @JsonIgnore.

Let’s see some basic examples to understand it more clear.

If we want to ignore multiple properties from both serialization and deserialization process then we can mention all property names at class level as below:-

@JsonIgnoreProperties({"gender","fullName"})
public class EmployeePojoWithJsonIgnoreProperties {

OR

@JsonIgnoreProperties(values = {"gender","fullName"})
public class EmployeePojoWithJsonIgnoreProperties {

We will create a POJO class in which some fields will be ignored using above annotation.

package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({"gender","fullName"})
public class EmployeePojoWithJsonIgnoreProperties {

        // 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;
        }
        
        
}
package JacksonTutorials;

import org.testng.annotations.Test;

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

public class SerialiDeserialExampleWithJsonIgnoreProperties {

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

                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnoreProperties);
                System.out.println("Serialization...");
                System.out.println(employeeJson);
                
        }
}
Serialization...
{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "age" : 29,
  "salary" : 12323.56,
  "married" : false,
  "eligibleForVote" : false
}

We have ignored properties “gender” and “fullName” which are not part of JSON output above.

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 SerialiDeserialExampleWithJsonIgnoreProperties {
        
        @Test
        public void deserializationWithJsonIgnoreProperties() 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();
                //objectMapper.con
                EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnoreProperties.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employeePojoWithJsonIgnoreProperties.getFirstName());
                System.out.println("Last name :- "+employeePojoWithJsonIgnoreProperties.getLastName());
                System.out.println("Age :- "+employeePojoWithJsonIgnoreProperties.getAge());
                System.out.println("Gender :- "+employeePojoWithJsonIgnoreProperties.getGender());
                System.out.println("Salary :- "+employeePojoWithJsonIgnoreProperties.getSalary());
                System.out.println("Married :- "+employeePojoWithJsonIgnoreProperties.getMarried());
                System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnoreProperties.getEligibleForVote());
                System.out.println("Full name :- "+employeePojoWithJsonIgnoreProperties.getFullName());
        }
}
Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 29
Gender :- null
Salary :- 12323.56
Married :- false
Eligible for vote :- false
Full name :- null

Similarly, during deserialization, fields “gender” and “fullName” are ignored and have default values while retrieving.

If we have a requirement to allow some fields of a POJO class only for serialization but not for deserialization then that we can achieve this by setting allowGetters as true for @JsonIgnoreProperties annotation. This setting will make fields read-only.

Note below that to add more than one elements i.e. list of properties to be ignored and value for allowGetters, you need to use with the element name.

package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {"gender","fullName"}, allowGetters = true )
public class EmployeePojoWithJsonIgnoreProperties {

        // 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;
        }
        
        
}


package JacksonTutorials;

import org.testng.annotations.Test;

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

public class SerialiDeserialExampleWithJsonIgnoreProperties {

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

                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnoreProperties);
                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
}

You can see in the above output fields were part of serialization as getters were allowed. You can ask that I said the above fields will be read-only but I can set value by calling setter methods. Read-only means you can not set the value using deserialization. See the next code.

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 SerialiDeserialExampleWithJsonIgnoreProperties {
        
        @Test
        public void deserializationWithJsonIgnoreProperties() 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();
                //objectMapper.con
                EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnoreProperties.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employeePojoWithJsonIgnoreProperties.getFirstName());
                System.out.println("Last name :- "+employeePojoWithJsonIgnoreProperties.getLastName());
                System.out.println("Age :- "+employeePojoWithJsonIgnoreProperties.getAge());
                System.out.println("Gender :- "+employeePojoWithJsonIgnoreProperties.getGender());
                System.out.println("Salary :- "+employeePojoWithJsonIgnoreProperties.getSalary());
                System.out.println("Married :- "+employeePojoWithJsonIgnoreProperties.getMarried());
                System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnoreProperties.getEligibleForVote());
                System.out.println("Full name :- "+employeePojoWithJsonIgnoreProperties.getFullName());
        }
}

Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 29
Gender :- null
Salary :- 12323.56
Married :- false
Eligible for vote :- false
Full name :- null

We have values for fields “Gender” and “Full name” in JSON string but while deserialization they were ignored as setters were not allowed.

If we have a requirement to allow some fields of a POJO class only for deserialization but not for serialization then that we can achieve this by setting allowSetters as true for @JsonIgnoreProperties annotation. This setting will make fields write-only.

package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {"gender","fullName"}, allowSetters = true )
public class EmployeePojoWithJsonIgnoreProperties {

        // 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;
        }
        
        
}
package JacksonTutorials;

import org.testng.annotations.Test;

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

public class SerialiDeserialExampleWithJsonIgnoreProperties {

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

                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employeePojoWithJsonIgnoreProperties);
                System.out.println("Serialization...");
                System.out.println(employeeJson);
                
        }
}

Serialization...
{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "age" : 29,
  "salary" : 12323.56,
  "married" : false,
  "eligibleForVote" : false
}
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 SerialiDeserialExampleWithJsonIgnoreProperties {

        @Test
        public void deserializationWithJsonIgnoreProperties() 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();
                //objectMapper.con
                EmployeePojoWithJsonIgnoreProperties employeePojoWithJsonIgnoreProperties = objectMapper.readValue(employeeString, EmployeePojoWithJsonIgnoreProperties.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employeePojoWithJsonIgnoreProperties.getFirstName());
                System.out.println("Last name :- "+employeePojoWithJsonIgnoreProperties.getLastName());
                System.out.println("Age :- "+employeePojoWithJsonIgnoreProperties.getAge());
                System.out.println("Gender :- "+employeePojoWithJsonIgnoreProperties.getGender());
                System.out.println("Salary :- "+employeePojoWithJsonIgnoreProperties.getSalary());
                System.out.println("Married :- "+employeePojoWithJsonIgnoreProperties.getMarried());
                System.out.println("Eligible for vote :- "+employeePojoWithJsonIgnoreProperties.getEligibleForVote());
                System.out.println("Full name :- "+employeePojoWithJsonIgnoreProperties.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

Since setters were allowed for ignored fields “gender” and “fullName”, they have participated in the deserialization process but not in serialization.

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: MSE-ReadyToUseSeleniumCode – Storing Web Table Data Into List Of Map – Java
Next Post: node tutorials

Related Posts

December 18, 2018 – Make Selenium Easy Uncategorized
DefineOrOptional – Make Selenium Easy Uncategorized
Introduction Of Page Object Model In Selenium WebDriver: Advantages And disadvatages Uncategorized
September 7, 2019 – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
August 17, 2019 – Make Selenium Easy 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