What is POJO?
POJO is sounds little fancy right and one of the important as well as interesting topic in
API. Its nothing but a simple java class which stands as Plain Old Java Object.
Its for Object representation in JSON Structure and the main purpose of the pojo class
is to increase the reusability and also improve the readability of the code and
easily understandable because of its simple code structure.
Simple JSON schema:
How to create a POJO class?
POJO class is created based on the request and response that we getting from the JSON pay load.
POJO class get all the keys from the JSON data and identify the variable and assign the exact variable in the
class like batch description as String, batch name as String and Batch number of classes as integer,
batch status as String .
create a sample POJO class for above simple JSON schema
In this POJO class after creating the variable as private and provides getters and setters methods
out of it for each method by pressing key SHIFT+ALT+S and select Generate Getters and Setters.
Always declare access modifier as private for the variable because it cant be manipulated outside the class.
Setter method --->Set the value to the variable
Getter method--->Get value of the variable
By Parsing JSON/XML values by achieving Serialization and Deserialization in POJO class.
What is Serialization and Deserialization?
Serialization : It is the process of converting class object into JSON object
ex:
Create an object for the batch details pojo class in the main class and
after creating object we can access the methods present in the pojo class.
batchdetails batch= new batchdetails();
 batch.setBatchdescription();
batch.setBatchname();
     batch.setBatchnoofclasses();
    batch.setBatchstatus();
    Â
By using the object we set the values using setter method ,
Now to serialize we should convert the pojo class batch object into JSON object
To achieve serialization and deserialization mechanism we need some libraries
for JSON are either JACKSON or JACKSON2 and for XML we need JAXB
How to get the dependencies?
In MVN repository search for JACKSON DATABIND copy the latest version and add in the
POM.xml
To handle JSON serialization and deserialization we use ObjectMapper object in java
ObjectMapper mapper= new  ObjectMapper ();
 Object Mapper converts the object into the string value.
code example:
Here parsing the batch class object to the mapper to converting Json data as String by
using writevalueAsString method and store the string value in a variable.
Create the Requestspecification to sending the request body(Jsonbody) payload as string and by
getting the response using Http Post method along with baseuri and cocntentType.
Http POST method ------>send data to the serverÂ
Json response:
"Json" : {
 "batchnoofclasses"=3,
 "batchdescription" ="apirestassured",
  "batchname"="apipros",
"batchstatus"="active"
}
    Â
     Deserialization :
It is the reverse of serialization that converts the JSON String into class object using
jackson library.
creating the POJO class with variables and getter and setter methods are common for both serialization and deserialization. Once we get the Json response (Jsonbody)back then use of deserialize mechanisam to convert that json response back into the class object(batchdetails).
For the reverse conversion in deserialization here we use ObjectMapper readvalue method.
ObjectMapper mapper =new ObjectMapper();
mapper.readvalue(null,null);
Now parsing json string and class into the method for conversion and store the object
in a variable as class name as return type.
mapper.readvalue(Jsonbody,batchdetails.class);
batchdetails details=mapper.readvalue(Jsonbody,batchdetails.class);
We can simply call the getter methods present in the main class for retrieve the values by
details.getBatchdescription();
details.getBatchname();
details.getBatchnoofclasses();
details.getBatchstatus();
Get all the batch details as output by printing the methods ,
apirestassured
apipros
3
active
How to Deal POJO classes with Nested Json?
Nested Json is little tricky compared to simple json body where the Json object that consist of
sub json objects .
The user first name, user last name ,user email ,age and address are the keys of the JSON body
But inside the address key there is another 3 propertise present in the JSON that is
street, city and province is called nested json.
Lets Create a POJO class for the Nested JSON:
main class
sub class
Here the POJO class is created with all the json keys( user firstname ,user lastname , user email,
age ) but for the address key we need to create another POJO class because of its nested sub json structure.
Once we created a seperate POJO class for address fields (street ,city , province) and generate the getter
and setter methods.
After create an addresspojo class for sub json fields we need to parse the class name as return type to the main class address key. Now we can generate the getter and setter methods for the main class pojo.
As we have discuss above we need to set values to the setter methods by create a java object for the class to serialize and deserialize.
Nestedpojo pojo= new Nestedpojo();
pojo.setUserfirstname("Raki");
pojo.setUserlastname("Saro");
pojo.setUseremail("Saroraki@gmail.com");
pojo.setAge(35);
When we try to set a value for the address field in the main class object it expecting addresspojo class object
and every other methods have string and int as return type.
So create class object for the subclass addresspojo and set the values for street,city and province.
addresspojo poj= new addresspojo();
poj.setStreet("Battel drive");
poj.setCity("Rochestor");
poj.setProvince("Newyork");
Now pass this subclass object variable to the main class as ,
pojo.setAddress(poj);
These way we can handle to set and get the values in the nested pojo class for serialization and deserialization.
Conclusion:
In this topic we discussed about how pojo classes created for simple json schema and nested json
and how to easily handle serialization and deserialization conversion with supporting JACKSON databind
library . Readers can easily go through the topic with each and every examples that is given and understand
the logic behind them and can easily apply it on their work.
Commentaires