Introduction
REST Assured is an Open Source library of Java for testing and validating REST APIs. It is a Java DSL for HTTP Builder that makes it easier to test REST-based services. Any HTTP method is supported by REST Assured, but it specifically supports POST, PUT, GET, DELETE, OPTIONS, PATCH, and HEAD. It also simplifies providing and validating arguments, headers, cookies, and body. The response to these requests can be used to validate and verify them.

As you are here to read this article, we assume you are familiar with REST APIs, but if you are not, you can check out our blog on REST API here. In this article, we will see Ignore unknown properties during deserialization in Rest assured in detail, so stay till the end!
Ignore Unknown Properties during Deserialization Using @JsonIgnoreProperties
In this section, we will see how unknown properties during deserialization create problems and how to avoid them using @JsonIgnoreProperties.
About @JsonIgnoreProperties
@JsonIgnoreProperties is an annotation used to either suppress the serialization of properties or avoid processing JSON properties read. This property can be used at an individual property level, such as @JsonIgnore, and a class level.
Required Java Library
As we are using Jackson API of Java, make sure we have the latest dependency of Jackson Databind in our project classpath.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
Note - For the latest version, click here.
Known and unknown properties
-
Known property: If a property is added to a POJO class, it is said to be a known property of a respective POJO class.
- Unknown property: If a property is not added to a POJO class or a property unfamiliar to a POJO class is said to be an unknown property.
Missing known properties during deserialization
Let's suppose we have the following POJO class:
package javaPackageName;
public class IgnoreUnknownPropertiesJsonIgnoreProperties {
/*Data members*/
private String badmintonBrand;
private String racketName;
/*Getters and Setters*/
public String getBadmintonBrand() {
return badmintonBrand;
}
public void setBadmintonBrand(String badmintonBrand){
this.badmintonBrand = badmintonBrand;
}
public String getRacketName() {
return racketName;
}
public void setRacketName(String racketName) {
this.racketName = racketName;
}
}
Deserialize with some missing known properties.
In the below code, we will not add known properties in JSON during deserialization:
package javaPackageName;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
public class MissingKnownProperties {
/*Data members*/
private String badmintonBrand;
private String racketName;
/*Getters and Setters*/
public String getBadmintonBrand() {
return badmintonBrand;
}
public void setBadmintonBrand(String badmintonBrand){
this.badmintonBrand = badmintonBrand;
}
public String getRacketName() {
return racketName;
}
public void setRacketName(String racketName) {
this.racketName = racketName;
}
@Test
public void MissingProperties() throws JsonMappingException, JsonProcessingException {
String badmintonString = "{\r\n" +
" \"badmintonBrand\" : \"Yonex\"\r\n" +
"}";
ObjectMapper objMap3r = new ObjectMapper();
MissingKnownProperties ClassObj = objMap3r.readValue(badmintonString, MissingKnownProperties.class);
System.out.println("Deserialization...Missing Known Properties");
System.out.println("Badminton Brand : "+ClassObj.getBadmintonBrand());
System.out.println("Racket Name : "+ClassObj.getRacketName());
}
}
Output:

In the above example, one known property, i.e., racket name, is missing. After deserialization, these fields give a default value (null). Therefore, no error is thrown for those missing values for known properties.
Deserialize with some additional unknown properties
package javaPackageName;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class IgnoreUnknownPropertiesJsonIgnoreProperties {
/*Data members*/
private String badmintonBrand;
private String racketName;
/*Getters and Setters*/
public String getBadmintonBrand() {
return badmintonBrand;
}
public void setBadmintonBrand(String badmintonBrand){
this.badmintonBrand = badmintonBrand;
}
public String getRacketName() {
return racketName;
}
public void setRacketName(String racketName) {
this.racketName = racketName;
}
@Test
public void MissingProperties() throws JsonMappingException, JsonProcessingException {
String badmintonString = "{\r\n" +
" \"badmintonBrand\" : \"Yonex\",\r\n" +
" \"racketName\" : \"Voltric 50 E-tune\",\r\n" +
" \"sponsors\" : \"TotalEnergies\"\r\n" +
"}";
ObjectMapper objMap3r = new ObjectMapper();
IgnoreUnknownPropertiesJsonIgnoreProperties ClassObj = objMap3r.readValue(badmintonString, IgnoreUnknownPropertiesJsonIgnoreProperties.class);
System.out.println("Deserialization...Ignoring Unknown Properties");
System.out.println("Badminton Brand : "+ClassObj.getBadmintonBrand());
System.out.println("Racket Name: "+ClassObj.getRacketName());
}
}
Output:

In the above example, we have added a property that is an unknown field, i.e., "sponsors". So, when we try to deserialize them, we can see that the deserialization failed at the encounter of unknown property, i.e., "sponsors". It gives a list of all known properties.
@JsonIgnoreProperties-ignoreUnknown
- IgnoreUnknown: Our motive is that if there are any unfamiliar fields in JSON, then the deserialization process should avoid them. And this can be achieved using a property called ignoreUnknown.