Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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.

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.

{
  "firstName": "Amod",
  "lastName": "Mahajan",
  "gender": "Male",
  "age": 28,
  "salary": 10000.56,
  "married": false
}

Let’s start creating POJO step by step.

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 :-

firstName - String
lastName - Sring
age - int
gender - String
salary - double
married - boolean

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.

package RestfulBookerPojo;

public class Employee {

        // private variables or data members of pojo class
        private String firstName;
        private String lastName;
        private String gender;
        private int age;
        private double salary;
        private boolean married; 
}

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.

// Getter and setter methods
        public String getFirstName() {
                return firstName;
        }
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
        public String getLastName() {
                return lastName;
        }
        public void setLastName(String lastName) {
                this.lastName = lastName;
        }
        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;
        }
        public double getSalary() {
                return salary;
        }
        public void setSalary(double salary) {
                this.salary = salary;
        }
        public boolean getMarried() {
                return married;
        }
        public void setMarried(boolean married) {
                this.married = married;
        } 

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.

package RestfulBookerPojo;

public class Employee {

        // private variables or data members of pojo class
        private String firstName;
        private String lastName;
        private String gender;
        private int age;
        private double salary;
        private boolean married;
        
        // Getter and setter methods
        public String getFirstName() {
                return firstName;
        }
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
        public String getLastName() {
                return lastName;
        }
        public void setLastName(String lastName) {
                this.lastName = lastName;
        }
        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;
        }
        public double getSalary() {
                return salary;
        }
        public void setSalary(double salary) {
                this.salary = salary;
        }
        public boolean getMarried() {
                return married;
        }
        public void setMarried(boolean married) {
                this.married = married;
        } 
}

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

package RestfulBookerPojo;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class EmployeeSerializationDeserialization {

        @Test
        public void createEmployeeJSONFromEmployeePOJOClass() throws JsonProcessingException
        {
                // Just create an object of Pojo class
                Employee employee = new Employee();
                // Set value as you wish
                employee.setFirstName("Amod");
                employee.setLastName("Mahajan");
                employee.setAge(29);
                employee.setGender("Male");
                employee.setSalary(3434343);
                employee.setMarried(false);
                
                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
                System.out.println(employeeJson);
        }
        
        
        @Test
        public void getPojoFromEmployeeObject() throws JsonProcessingException
        {
                // Just create an object of Pojo class
                Employee employee = new Employee();
                // Set value as you wish
                employee.setFirstName("Amod");
                employee.setLastName("Mahajan");
                employee.setAge(29);
                employee.setGender("Male");
                employee.setSalary(3434343);
                employee.setMarried(false);
                
                // Converting a Java class object to a JSON payload as string
                ObjectMapper objectMapper = new ObjectMapper();
                String employeeJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
                
                
                // Converting EMployee json string to Employee class object
                Employee employee2 = objectMapper.readValue(employeeJson, Employee.class);
                System.out.println("First Name of employee : "+employee2.getFirstName());
                System.out.println("Last Name of employee : "+employee2.getLastName());
                System.out.println("Age of employee : "+employee2.getAge());
                System.out.println("Gender of employee : "+employee2.getGender());
                System.out.println("Salary of employee : "+employee2.getSalary());
                System.out.println("Marital status of employee : "+employee2.getMarried());
        }
}

{
  "firstName" : "Amod",
  "lastName" : "Mahajan",
  "gender" : "Male",
  "age" : 29,
  "salary" : 3434343.0,
  "married" : false
}
First Name of employee : Amod
Last Name of employee : Mahajan
Age of employee : 29
Gender of employee : Male
Salary of employee : 3434343.0
Marital status of employee : false

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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 47: Marking a Parameter as Optional in TestNG
Next Post: Interview Experience

Related Posts

July 14, 2018 – Make Selenium Easy Uncategorized
October 12, 2019 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
July 14, 2017 – Make Selenium Easy Uncategorized
Authentication vs Authorization – Who vs Who+What Uncategorized
December 20, 2017 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark