Make Selenium Easy

REST Assured Tutorial 38 – How Getter & Setter methods Matter For Serialization Deserialization Using POJO

As a part of End to End REST Assured Tutorial, in this post, we will see how getter and setter methods of a POJO classes are related to serialization and deseriazation process.

A typical POJO class consists of private fields with its getter and setter methods or accessors (Which is not mandatory). You can have fields and accessors with any access level in a POJO. In this post, we will see how presence and absence of getter or setter methods impacts serialization and deserialization process.

POJO with proper getter and setter methods

Below is a POJO class with some private properties and their public accessors i.e. getter and setter methods. I have put a print statement in all getter and setter methods just to show method calling during serialization and deserialization process.

Let’s serialize with the above POJO class.

Output

Since we are calling setter methods to set values of properties so setter methods are executed. While serializing, getter methods are called internally as you can see in the output. That seems to be very obvious as to form a JSON they need a value of fields that can be retrieved using getter methods.

Let’s deserialize with the above POJO class.

Output

During deserialization, setter methods are called internally as you can see in output as well. To print values, we are explicitly calling getter methods which you can see in the output.

In short:-

Serialization uses Getter methods.

Deserialization uses Setter methods

Let’s see what happens when setter methods are absent.

POJO with only getter methods

I removed all the traditional setter methods of properties. We have just setter methods now in the POJO class.

Let’s serialize with the above POJO class.

Output

Since we do not have setter methods we can not serialize with expected values. All class level fields are initiaized with default values which will be used for serialization as you can see in output.

Let’s deserialize with the above POJO class.

Output

Deserialization had no issues with the absence of setter methods. If you see deserialization example of POJO with both getter and setter methods, in output setter methods were called internally which is not the same case in this example.

In short:-

If you have only getter methods in POJO class, you can not serialize it with desired values but can deserialize it easily. You do not need dedicated setter methods for deserializing.

Let’s see what happens when the getter methods are absent.

POJO with only setter methods

Let’s serialize with the above POJO class.

Output

We got an exception stating “No serializer found” which is obvious as serialization looks for getter methods that are not available.

Let’s deserialize with the above POJO class.

Output

Since we do not have getter methods so we can not retrieve values of properties but that does not mean that properties are not initialized. Let’s add other getter methods to fetch values.

Updated deserialize code

Output

So if you have only setter methods then you can not serialize even you have any alternative getter methods. With setter methods, you can deserialize it easily but you need alternate getter methods to retrieve values.

If you do not have traditional or intended getter methods then you can not serialize and deserialize it.

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 38 – How Getter & Setter methods Matter For Serialization Deserialization Using POJO

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