Make Selenium Easy

REST Assured Tutorial 29 – How to create POJO classes of a JSON Object Payload

Last updated on June 10th, 2020 at 07:42 am

As a part of End to End REST Assured Tutorial , in this post We will learn to create a simple POJO class for a simple JSON Object payload.

POJO classes are extensively used for creating JSON and XML payloads for API. Although there are many online platform to generate POJO and Java libraries to generate POJO classes automatically but still knowing to create POJO helps. Even you can use plain POJO concepts or POJO with builder pattern for data storage as well which we have seen in last post where I have stored employee data.

Although there is no restrictions on the way of creating POJO but creating with some guidelines help which we will see in this post.

Very simple JSON example

Below is a JSON with some nodes which is actually a 1:1 mapping i.e. each key has single value and type of values are mixed.

Let’s start creating POJO step by step.

Identify fields or variables for POJO class

Observe in example JSON, we have fields firstName, lastName , gender, age , salary and married. Each field has a corresponding values. Now focus on data types of values. firstName and lastName has String values while age has integer value, salary has double value and married has boolean. Let’s map data type with field name as below :-

Let’s create variables in POJO class now. Since it is storing a employee detail, name POJO class as Employee and make all variables (at least for this example and it is beginning ) as private so that no one can manipulate it directly.

Add getter and setter methods for private variables

Since we have created all variables as private then there should be a way to manipulate or retrieve these data. Let’s add getter and setter methods for each variables. It is not mandatory to have getter and setter for all and depends upon need. But as of now let’s add getter and setter for all in same class. A getter method is to get value of a variable and a setter method is to set value of a variable from outside.

If you are worrying that it is painful to create getter and setter methods manually for a large number of variables then do not worry. Every IDE gives you a shortcut to generate getter and setter methods. Just google it out.

Let’s do not add any constructors in class so that it will have a default constructors.

Complete POJO class

We are done now. We have created a POJO class of employee JSON. Now you must be thinking how this POJO class will help now?

Using above POJO class you can create any number of custom Employee objects and each object can be converted in to a JSON Object and Each JSON object can be parsed in to Employee POJO. If you have an API which requires dynamic payload of Employee then you can easily create as many as required employee payloads with different data in stead of creating hard coded JSON objects. In simple words POJO gives you flexibility of creating and manipulating data in simple ways.

We will create a JSON object form POJO and vice versa now which is generally called as serialization and deserialization using Jackson APIs. Do not worry about the terms serialization and deserialization as of now as I have not covered it yet. For time being, you just know :-

serialization – Convert Employee class object to JSON representation or Object

deserialization – reverse of serializing . Convert a JSON Object to Employee class object

Output

You can download/clone 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 post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through 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

3 thoughts on “REST Assured Tutorial 29 – How to create POJO classes of a JSON Object Payload

  1. Hi Amod,

    Very elaborated and free flow explanation above. I would recommend you to provide library/dependency details in form of download link/maven/gradle. Like, here you have mentioned about usage of Jackson APIs. Even though if you have already mentioned corresponding link in some other post, but still it will help readers to have it handy. It is just my personal opinion.

    Regards,
    Lajish

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 29 – How to create POJO classes of a JSON Object Payload

by Amod Mahajan time to read: 5 min
3
%d bloggers like this: