Table of contents
1.
Introduction📃
2.
REST Assured💢
3.
Ignore Default Values In Payload✍️
3.1.
@JsonInclude Annotation
3.2.
What Happens if we do not Include the @JsonInclude annotation
3.3.
Including the @JsonInclude Annotation
4.
Ignore Null Values In Payload✍️
5.
Frequently Asked Questions
5.1.
What is API?
5.2.
What is REST API?
5.3.
What is REST Assured?
5.4.
What is JSON?
5.5.
What is @JsonInclude annotation?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Ignore Default and Null values in Payload in Rest Assured

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

Introduction📃

Since you are reading this blog, I think you are familiar with REST APIs. But if you are unfamiliar with REST API, you can learn from this blog on REST API

Rest API

In this blog, we will learn how to Ignore Default and Null values in Payload in Rest Assured. But Before learning how to Ignore Default and Null values in Payload in Rest Assured, let’s get familiar with REST Assured. 

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. 

REST Assured

Any HTTP method is supported by REST Assured, but it specifically supports POSTGETPUTDELETEOPTIONSPATCH, and HEAD. It also simplifies providing and validating things like arguments, headers, cookies, and body. The response to these requests can be used to validate and verify them.

Let’s learn how to ignore default values in Payload.

Ignore Default Values In Payload✍️

Sometimes we don’t want the default values that came automatically in the Payload. To ignore the default values, we can use the @JsonInclude Annotation at the class or property level.

Let’s explore more about @JsonInclude Annotation.

@JsonInclude Annotation

The @JsonInclude Annotation is provided by Jackson API. It is used to specify whether the value of the annotated property (when used as a field, method, or constructor parameter) or all properties of the annotated class should be serialized. Property values are always included without the annotation, but one can provide easy exclusion rules with it to limit the number of properties to write out.

There are multiple options in Enum Include, shown in the below table:-

ENUM INCLUDE OPTIONS

Let’s see how to use @JsonInclude Annotation to ignore default values in Payload.

Since the @JsonInclude annotation is provided by Jackson API, we need to add the Jackson library to our project. We are using maven as a build tool here.

<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

What Happens if we do not Include the @JsonInclude annotation

Let’s say we have a Student class as given below:-

package CodingNinjas;

public class Student {

/*Private Variables of Student class.*/
private String first_name;
private String last_name;
private String gender;
private int age;

/*Getter and Setter methods*/
public String getfname() {
return first_name;
}

public void setfname(String first_name) {
this.first_name = first_name;
}

public String getlname() {
return last_name;
}

public void setlname(String last_name) {
this.last_name = last_name;
}

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


It is not always necessary to pass values to all fields in a payload. For example, you may not need to set the value for the "age" field in the payload if it is optional.

Converting Java Object to JSON

package CodingNinjas;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 
 
public class NinjasJsonIncludeExample {
 
public static void main(String[] args) throws JsonProcessingException {
 
/* Create an object of Student class.*/
Student student = new Student();

/*Set values*/
student.setfname("Rajat");
student.setlname("Agrawal");
student.setGender("Male");
/*student.setAge(22);*/

/*Converting an object to a JSON payload as string.*/
ObjectMapper objectMapper = new ObjectMapper();
String studentJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);
System.out.println(studentJson);
}
}
You can also try this code with Online Java Compiler
Run Code


Here we can see that we are not setting the Age variable. 

Output:

{
  "first_name" : "Rajat",
  "last_name" : "Agrawal",
  "gender" : "Male",
  "age" : 0
}
You can also try this code with Online Java Compiler
Run Code


Here you can see how the default value for an integer field Age was taken as zero when we did not set any value to it.

Let’s use the @JsonInclude annotation and ignore the default value for Age.

Including the @JsonInclude Annotation

Here, we include the @JsonInclude(JsonInclude.Include.NON_DEFAULT) at the class level to ignore any default value.

package CodingNinjas;

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Student {

/*Private Variables of Student class.*/
private String first_name;
private String last_name;
private String gender;
private int age;

/*Getter and Setter methods*/
public String getfname() {
return first_name;
}

public void setfname(String first_name) {
this.first_name = first_name;
}

public String getlname() {
return last_name;
}

public void setlname(String last_name) {
this.last_name = last_name;
}

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


Output:

{
  "first_name" : "Rajat",
  "last_name" : "Agrawal",
  "gender" : "Male"
}
You can also try this code with Online Java Compiler
Run Code

Now the output will look like above, where the unset Age value is ignored.

Ignore Null Values In Payload✍️

Sometimes we don’t want the null values that came automatically in the Payload. We can use the @JsonInclude Annotation to ignore the null values at the class or property level.

In Java, a default value is provided to class variables. NULL is the most dangerous default value. We may get NullPointerException if we do not provide checks or validation for null values. Ignoring all default values isn't always a good idea, and sometimes we only want to ignore NULL values. In that case, we can use the Include enum NON_NULL as seen below:-

package CodingNinjas;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Student {

/*Private Variables of Student class.*/
private String first_name;
private String last_name;
private String gender;
private int age;

/*Getter and Setter methods*/
public String getfname() {
return first_name;
}

public void setfname(String first_name) {
this.first_name = first_name;
}

public String getlname() {
return last_name;
}

public void setlname(String last_name) {
this.last_name = last_name;
}

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


Converting Java Object to JSON

package CodingNinjas;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
 
 
public class NinjasJsonIncludeExample {
 
public static void main(String[] args) throws JsonProcessingException {
 
/*Create an object of Student class.*/
Student student = new Student();

/*Set values*/
student.setfname("Rajat");
/*student.setlname("Agrawal");*/
student.setGender("Male");
student.setAge(22);

/*Converting an object to a JSON payload as string.*/
ObjectMapper objectMapper = new ObjectMapper();
String studentJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);
System.out.println(studentJson);
}
}
You can also try this code with Online Java Compiler
Run Code


Here we did not set the last_name field. 

Output:

{
  "first_name" : "Rajat",
  "gender" : "Male",
  "age" : 22
}
You can also try this code with Online Java Compiler
Run Code


Now the output will look like above, where the unset last_name default value null is ignored.

Frequently Asked Questions

What is API?

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

What is REST API?

REST (Representational State Transfer) is an architectural paradigm for establishing web services that define a set of 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 that can be read by humans to store and send data objects made up of arrays and attribute-value pairs.

What is @JsonInclude annotation?

The @JsonInclude Annotation is provided by Jackson API. It is used to specify whether the value of the annotated property (when used as a field, method, or constructor parameter) or all properties of the annotated class should be serialized.

Conclusion

In this article, we have extensively discussed how to ignore default and null values in Payload in Rest Assured. I hope you enjoyed reading this article on Ignore Default and Null values in Payload in Rest Assured.

If you want to learn more, check out our articles on Implementing DELETE Method to Delete a User ResourceTechnological Services in Ready APIWhat Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2pyThird Party Modules In Web2pyTasks In Web2py, and  XML in Web2py.

Also, check out these exciting courses from coding ninjas to expand your knowledge, Coding CourseCode StudioInterview ExperienceGuided PathInterview ProblemsTest SeriesLibrary, and Resources

Happy Coding!

Live masterclass