Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
Any HTTP method is supported by REST Assured, but it specifically supports POST, GET, PUT, DELETE, OPTIONS, PATCH, 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:-
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.
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
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
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.