Make Selenium Easy

REST Assured Tutorial 39 – @JsonIgnore Annotation of Jackson API – Exclude Field of Pojo From Serialization and Deserialization

As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation called @JsonIgnore of Jackson library which helps in restricting properties of a POJO class from serialization and deserialization.

Prerequisite

Since we are using Jackson API of Java for this example, make sure you have the latest dependency of Jackson Databind in your project classpath. I have used below Jackson dependency for this post:-

There may be multiple fields in a Pojo class and sometimes you don’t require some fields to participate in serialization and deserialization process. I can give a scenario where we may need to ignore some properties of a Pojo class.

In a POJO class, we may have properties whose values are derived from other properties. For example- If we know the first name and last name of a person we can get their full name. If we know the age of a person we can get to know if a person is eligible for the vote or not. We can have getter methods for such properties to return values but should not be allowed to set from outside.

Consider below POJO.

Serialization using above POJO

Output

Deserialization using above POJO

Output:-

If you observe output carefully we see values of properties of “fullName” and “eligibleForVote” have set wrongly. We should have calculated it. Ideally, these fields should have getter methods only just for quick access.

Complete Program

There may be some fields that may be optional or just want to ignore it for time being.

If we want to ignore any property of a POJO class from serialization and deserialization we can use @JsonIgnore provided by Jackson API on that property. It is a marker annotation that indicates that the logical property that the accessor is to be ignored by introspection-based serialization and deserialization functionality. Annotation only needs to be added to one of the accessors (often getter method, but may be setter, field or creator parameter), if the complete removal of the property is desired.

POJO with JsonIgnore

Serialization using POJO with JsonIgnore

Output:-

Deserialization using POJO with JsonIgnore

Output

Complete program

We have values for fields fullName and eligibleForVote in Json but it has not been deserialized as you can see it has default values not from Json.

@JsonIgnore annotation can be used with getter and setter methods as well.

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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

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.