Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 41 – Ignore unknown properties during deserialization Using @JsonIgnoreProperties

Posted on 02/19/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, We will learn about how unknown properties during deserialization creates a problem and how can we avoid them using @JsonIgnorePropertoies.

@JsonIgnoreProperties Annotation Of Jackson API – Exclude Field Of Pojo From Serialization Or Deserialization Or Both

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

Before we go for an actual topic let’s understand the meaning of words “known” and “unknown” properties. If a property is included in a POJO class or if a property exists in POJO class then it is a known property for that POJO class. A property that is not known to a POJO class is unknown. Let’s see the behavior when a known property is missing during deserialization.

Suppose we have a POJO as below:-

package JacksonTutorials;

public class Employee {

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

We will not include all known properties in JSON during deserialization as shown below:-

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 DeserializationWithMissingKnownProperties {

        @Test
        public void deserializationWithMissingKnownProperties() throws JsonMappingException, JsonProcessingException
        {
                String employeeString = "{\r\n" + 
                                "  \"firstName\" : \"Amod\",\r\n" + 
                                "  \"lastName\" : \"Mahajan\",\r\n" + 
                                "  \"gender\" : \"Male\"\r\n" +
                                "}";
                
                ObjectMapper objectMapper = new ObjectMapper();
                //objectMapper.con
                Employee employee = objectMapper.readValue(employeeString, Employee.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employee.getFirstName());
                System.out.println("Last name :- "+employee.getLastName());
                System.out.println("Age :- "+employee.getAge());
                System.out.println("Gender :- "+employee.getGender());
                System.out.println("Salary :- "+employee.getSalary());
                System.out.println("Married :- "+employee.getMarried());

        }
}
Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 0
Gender :- Male
Salary :- 0.0
Married :- false

In JSON string three known properties are missing i.e. age, salary, and married. After deserialization, these fields have a default value. There is no error thrown for those missing values for known properties.

In the previous example, we had missing known properties in our JSON. Now we will have some additional unknown properties in JSON and observe behavior during deserialization.

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 DeserializationWithAdditionalUnknownProperties {

        @Test
        public void deserializationWithAdditionalUnknownProperties() 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
                Employee employee = objectMapper.readValue(employeeString, Employee.class);
                System.out.println("Deserialization...");
                System.out.println("First name :- "+employee.getFirstName());
                System.out.println("Last name :- "+employee.getLastName());
                System.out.println("Age :- "+employee.getAge());
                System.out.println("Gender :- "+employee.getGender());
                System.out.println("Salary :- "+employee.getSalary());
                System.out.println("Married :- "+employee.getMarried());

        }
}

There are some additional unknown fields that are present in JSON string i.e. fullName and eligibleForVote. When we try to deserialize above JSON string we get the output as below:-

[RemoteTestNG] detected TestNG version 7.0.1
FAILED: deserializationWithMissingKnownProperties
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "fullName" (class JacksonTutorials.Employee), not marked as ignorable (6 known properties: "lastName", "married", "salary", "firstName", "age", "gender"])
 at [Source: (String)"{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : "Male",
  "age" : 29,
  "salary" : 12323.56,
  "married" : false,
  "fullName" : "Amod Mahajan Gupta",
  "eligibleForVote" : false
}"; line: 8, column: 17] (through reference chain: JacksonTutorials.Employee["fullName"])

As you can see deserialization failed at the encounter of first unknown property “fullName”. It also gives a list of all known properties.

We want that if there are any unknown fields in JSON then the deserialization process should ignore them. This we can achieve using a property called “ignoreUnknown” of @JsonIgnoreProperties annotation. It accepts a boolean value and the default value is false. To ignore unknown property we need to set it true at the class level.

package JacksonTutorials;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee {

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

There is no change in the above deserialization code.

Deserialization...
First name :- Amod
Last name :- Mahajan
Age :- 29
Gender :- Male
Salary :- 12323.56
Married :- false

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: Java Collection Interview Questions
Next Post: Protractor Tutorial 5 – Introduction & Installation of NodeJS

Related Posts

Postman Tutorial Part 42 – Create Parameterized Request by Reading Data From CSV in Postman – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
TestNG Tutorials 9: Internal Logic Of Generation Of TestNG.xml Uncategorized
How To Locate Web Element Which Has Multiple Class Names Uncategorized
image – Make Selenium Easy Uncategorized
Selenium Interview Experience 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