Make Selenium Easy

REST Assured Tutorial 37 – @JsonInclude Annotation – Ignore Null & Empty Values In Payload

As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation @JsonInclude of Jackson library which helps in eliminating default, null, empty, etc values. For this post, we will focus on excluding null and empty values in a payload.

Maven Dependency

Since we are going to use an annotation provided by Jackson API, we need to add the Jackson library in our project. I am using maven as a build tool here.

Always try to use the latest dependency from the Central Maven Repository. I will use the below dependency at the time of writing this post.

We have already learned about @JsonInclude annotation and different values of Include enum in the previous post.

Usage of NON_NULL

In Java, a default value is provided to class variables. A chart is below:-

Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char‘u0000’
String (or any object)null
booleanfalse

The most dangerous default value is NULL. If we do not put checks or validation of null values we may end up with NullPointerException. Sometimes ignoring all default values is not good and we just want to ignore NULL values. In that case, we can use NON_NULL of Include enum as shown below:-

Converting Java Object to JSON Object

Output

We have not set values for fields ‘lastName’, ‘gender’,’age’ and ‘salary’. Since default values of fields ‘lastName’ and ‘gender’ are NULL which will be ignored during serialization but default values of fields ‘age’ and ‘salary’ are zero, so they will be included in JSON which you can see in above output.

Usage of NON_EMPTY

The scope of NON_EMPTY is greater than NON_NULL or we can say NON_EMPTY includes NON_NULL in it with others. As per the official document:-

NON_EMPTY + NON_NULL + NON_ABSENT

Properties of a class will not be included during serialization if the value of the property is NULL or Empty. NULL we have already seen. It is for String and reference variables. EMPTY is a little tricky. The definition of emptiness depends on the data type of field.

  1. A NULL value is also considered as EMPTY.
  2. For Collections and Map, isEmpty() method is called to check emptiness i.e. without any content.
  3. For an array and string, the length is checked for emptiness.

A POJO with different data types:-

Let’s not set any value for any field of POJO class:-

Output:-

We can see how fields are initialized with default values. Obviously we can ignore NULL values using NON_NULL which we have seen above. Let’s add NON_NULL at the class level and create an object as below:-

Output

You can see NON_NULL did not serve the purpose here as we have empty values not null.

Let’s add NON_EMPTY at class level as:-

Let’s rerun both @Test methods again:-

Output of first @Test Method:-

Output of second @Test Method:-

I have use annotation at class level but you can use it at property level as well.

Complete Program

Output (When NON_EMPTY added at class level)

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 37 – @JsonInclude Annotation – Ignore Null & Empty Values In Payload

by Amod Mahajan time to read: 6 min
0
%d bloggers like this: