Table of contents
1.
Introduction
2.
Ignore Unknown Properties during Deserialization Using @JsonIgnoreProperties
2.1.
About @JsonIgnoreProperties
2.2.
Required Java Library
2.3.
Known and unknown properties
2.4.
Missing known properties during deserialization
2.5.
Deserialize with some missing known properties.
2.6.
Deserialize with some additional unknown properties
2.7.
@JsonIgnoreProperties-ignoreUnknown
3.
Ignore Unknown Properties During Deserialization Using ObjectMapper-Jackson API
3.1.
ObjectMapper configuration Code
4.
Frequently Asked Questions
4.1.
What is API?
4.2.
What is REST API?
4.3.
What is REST Assured?
4.4.
What is JSON?
4.5.
What is @JsonIgnoreProperties annotation?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Ignore Unknown Properties During Deserialization in Rest Assured

Author yuvatimankar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Ignore unknown properties during deserialization

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>
You can also try this code with Online Java Compiler
Run Code

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;
    }
    
}
You can also try this code with Online Java Compiler
Run Code

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());
	}
}
You can also try this code with Online Java Compiler
Run Code


Output:

output image

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());
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

Output image

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.

Ignore Unknown Properties During Deserialization Using ObjectMapper-Jackson API

Until now, we avoided unknown properties during deserialization by using @JsonIgnoreProperties which functions at the class level. But, if we cannot access the POJO class or cannot edit POJO classes, then we can go for ObjectMapper-level configuration.

For all this to work, we need to enum DeserializationFeature to set the value of “FAIL_ON_UNKNOWN_PROPERTIES” as false.
 

Note - Enumeration: defines on/off features that affect the way the Java objects are deserialized from the JSON.

ObjectMapper configuration Code

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.databind.DeserializationFeature;

public class IgnoreUnknownPropertiesObjMap3r {
	
	/*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();
		objMap3r.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		IgnoreUnknownPropertiesObjMap3r ClassObj = objMap3r.readValue(badmintonString, IgnoreUnknownPropertiesObjMap3r.class);
		System.out.println("Deserialization...Ignoring Unknown Properties");
		System.out.println("Badminton Brand : "+ClassObj.getBadmintonBrand());
		System.out.println("Racket Name: "+ClassObj.getRacketName());
	}
}
You can also try this code with Online Java Compiler
Run Code


Output:

output image

Here, we have used the enum DeserializationFeature to set the value of "FAIL_ON_UNKNOWN_PROPERTIES = false". We don't have to make any changes in the POJO class. Now, we can see how deserialization occurred successfully with unknown properties present in the JSON string.

Frequently Asked Questions

What is API?

API stands for Application Programming Interface. It is a set of protocols and definitions for creating and integrating software applications. 

What is REST API?

REST (Representational State Transfer) is an architectural paradigm for establishing web services that define requirements. REST API is a straightforward and flexible approach to accessing online services without going through any processing.

What is REST Assured?

REST Assured is an Open Source Java library for testing and validating REST APIs. It is a Java DSL for HTTP Builder that makes it easier to test REST-based services.

What is JSON?

JSON is a free and open-source file format and data exchange format that uses text humans can read to store and send data objects made up of arrays and attribute-value pairs.

What is @JsonIgnoreProperties annotation?

@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.

Conclusion

In this article, we have discussed ignoring unknown properties during deserialization using @JsonIgnoreProperties and ObjectMapper-Jackson API in Rest Assured. We started with the introduction of Rest Assured, then saw Ignore Unknown Properties during Deserialization Using @JsonIgnoreProperties and then Ignore Unknown Properties During Deserialization Using ObjectMapper-Jackson API.

If you want to read more on such topics, you can check out our related blogs:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy learning, Ninja

Live masterclass