It’s very likely that you could have a requirement that you have a POJO (Plain Old Java Object) and you need to send it to the API call. To do that you need to ‘Serialize’ it in Rest-Assured and this process is referred as ‘Serialization’. While on the other hand, you have the API response and you would need to ‘Deserialize’ it into a POJO then it’s called ‘De-Serialization’. Now, In this tutorial, we will learn“How to perform Serialization and De-serialization in Rest Assured?”.
Simple representation of Serialization and De-serialization using RESTAssured...!!! Share on X
In this tutorial, we are going to cover the below topics:
- Serialization of POJO into a JSON Request Body Object
- De-Serialization of API Response to a POJO
But, first let’s understand what are these concepts ‘Serialization and Deserialization’ actually mean in Java.
What is Serialization and Deserialization?
Serialization is a process of conversion of the Instance of a Class (state of the Java object) to a byte stream. Then, this serialized object or we say this Byte steam can be stored in files or external sources and can be transferred over networks. While on the other hand, Deserialization is simply the reverse of Serialization meaning converting the byte stream back to the Java object.
To achieve Serialization, a class needs to implement Serializable Interface and such class are actually Java Beans or say POJO (Plain Old Java Object). So, basically Serialization is the process of Converting a POJO to a JSON object and converting a JSON object back to POJO is called Deserialization.
Let’s begin:
1. Serialization of POJO into a JSON Request Body Object
Let’s take an example, Here we are using a ‘Student’ class as a POJO which is holding some basic attributes like name, batch, and roll number.
Student.java (POJO)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public class Student { String name; String batch; int rollNumber; public Student(String name, String batch, int rollNumber) { this.name=name; this.batch=batch; this.rollNumber=rollNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBatch() { return batch; } public void setBatch(String batch) { this.batch = batch; } public Integer getRollNumber() { return rollNumber; } public void setRollNumber(Integer rollNumber) { this.rollNumber = rollNumber; } public String toString(){ return "The Student name is " + this.name + " studies in Batch " + this.batch + "and has rollNumber " + this.rollNumber; } } |
Now our POJO is ready to use, so we will move onto our main step of creating a Rest Assured Test to perform the serialization bypassing this POJO to the API.
Here I am using an API that I build in my local machine to support this example which is having a POST Endpoint on which we are making a call request with the above-mentioned POJO (in the form of the object instance) in the Request body object.
As I’ve mentioned this line above “in the form of the object instance”, it means we need to create a sample object instance of Student (POJO) class and specify some values to it. Like below:
Student student = new Student(“Isha Durani”, “A”, 101);
Now, let’s create the test class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; public class TestSerialization { @Test public void testSerialization() { Response response = null; Student student = new Student("Isha Durani", "A", 101); response = RestAssured.given() .contentType("application/json") .body(student) .when() .post("http://localhost:9091/students"); assertTrue(response.toString().contains("Student added successfully.")); } } |
When you’ll execute this above class, Rest-Assured will render the ‘Student’ object instance into the JSON formatted request body object. Like below:
1 2 3 4 5 | { "name": "Isha Durani", "batch": "A", "rollNumber": 101 } |
That’s it. It’s that simple to serialize the Java Object (POJO) to a JSON Request object.
2. De-Serialization of the API Response into a POJO
As we have seen above in point number 1, we passed POJO as a Java object to the API Request. But now, we are doing the reverse by transforming the API response to a POJO Java instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import org.testng.annotations.Test; import io.restassured.RestAssured; public class TestDeSerialization { @Test public void testDeSerialization() { Student student = RestAssured.get("http://localhost:9091/students/1").as(Student.class); System.out.println(student.toString()); } } |
Notice that “http://localhost:9091/students/1” is the GET Endpoint. Here on this Endpoint, we are looking for a student having id as 1. You can set up any rest GET API endpoint for testing purposes.
Below is the output of the execution of TestDeSerialization class:
1 | The Student name is Isha Durani studies in Batch A and has rollNumber 101 |
That’s it. It’s that simple to de-serialize the API Response into a Java Object (POJO).
Simple representation of Serialization and De-serialization using RESTAssured...!!! Share on X
If you like this post, please check out my other useful blog posts on Rest Assured:
Other Useful References:
Hi Deepak, I was trying the serialization / deserialization with your example shown here but got stuck in the deserialization. I guess the json data is not getting extracted and shows an exception as below. –
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
serialization.Users
(no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)at [Source: (String)”{
“firstName”: “Hikaru”,
“lastName”: “Nakamura”,
“fideRating”: 2727,
“id”: 26
}”; line: 2, column: 3]
Please let me know what needs to be done to fix this issue. I’m using jackson data binding.
Hello Deepak, Very good article…..can you suggest how big and complex json payloads are handled…For eg like below-
https://jsonplaceholder.typicode.com/users
Thanks for appreciating the article, Nakshtra.
There is no restriction mentioned on the website.
But, if you experience any issue with the larger payload, let me know.
Thanks
Thanks for the write-up. It would have been extremely useful if your “Student” example included slightly more complex nested JSON. Nevertheless, thanks for sharing.
I am glad you liked it Andy.
And, thanks for the valuable feedback. I kept a note of your suggestion and I’ll publish a post on nested json example as soon as possible.
Thanks again.
Thanks for sharing interesting and informative post.
Thank you. I’m glad it helped.