What is a POJO Class?
POJO stands for Plain Old Java Object.
A POJO is a simple Java class that contains:
- Private variables (fields)
- Getters
- Setters
- Constructors (optional)
- No special inheritance or framework dependency
In REST Assured, POJO classes are commonly used to:
- Create request payloads
- Map JSON responses to Java objects
- Improve code readability and maintainability
Interview Answer
"POJO stands for Plain Old Java Object. It is a simple Java class with private fields, getters, and setters. In REST Assured, I use POJO classes to represent API request bodies and to deserialize JSON responses into Java objects. This makes the code cleaner, reusable, and type-safe."
POJO Structure
POJO Class
│
├── Private Variables
├── Getters
├── Setters
├── Constructors
└── Optional toString()
Sample POJO Class
public class User {
private String name;
private String email;
private int age;
public User() {
}
public User(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
How POJO Is Used in REST Assured
Java Object (POJO)
│
▼
Serialization
│
▼
JSON Request
│
▼
REST API
│
▼
JSON Response
│
▼
Deserialization
│
▼
Java Object (POJO)
Real-Time Example
In my project, POJO classes represented entities such as users and products. I used them as request bodies for Create and Update APIs, and I deserialized API responses back into POJO objects for validation using Java getters.
Advantages of Using POJO
POJO classes provide a clean and structured way to handle request and response data.
Benefits
- Type safety
- Reusable request objects
- Easy JSON-to-Java mapping
- Easy Java-to-JSON conversion
- Cleaner code
- Better maintainability
- Easy integration with Jackson and Gson
- Reduced manual JSON creation
POJO vs Manual JSON
| Manual JSON | POJO |
|---|---|
| JSON written manually | Java object represents payload |
| Higher chance of typing mistakes | Type-safe |
| Harder to maintain | Easy to maintain |
| Difficult for large payloads | Better for complex payloads |
| Limited code reuse | Highly reusable |
Real-Time Example
Using POJO classes allows us to convert Java objects into JSON automatically and map JSON responses back into Java objects. This avoids manually writing JSON strings and significantly reduces errors.
What is Serialization?
Serialization is the process of converting a Java object (POJO) into a JSON document.
The generated JSON is sent as the request payload to the API.
Serialization Flow
Java Object
│
▼
Serialization
│
▼
JSON Payload
│
▼
REST API
POJO Object
User user = new User();
user.setName("John");
user.setEmail("john@test.com");
user.setAge(28);
Automatic Serialization
REST Assured automatically converts the POJO into JSON when:
- The request body contains a POJO.
- The
Content-Typeis set toapplication/json.
Example:
given()
.contentType(ContentType.JSON)
.body(user)
.when()
.post("/users");
REST Assured internally serializes the Java object into JSON.
Generated JSON
{
"name":"John",
"email":"john@test.com",
"age":28
}
Manual Serialization Using Jackson
Sometimes manual serialization is required.
Example:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
Manual Serialization Using Gson
Gson gson = new Gson();
String json = gson.toJson(user);
What Should You Verify During Serialization?
Ensure that:
- The POJO has proper getters and setters.
- Field names match the API contract.
- Nested objects serialize correctly.
- Collections are converted correctly.
- The generated JSON matches the expected schema.
Real-Time Example
In our framework, REST Assured automatically serializes POJO objects into JSON whenever we pass them to the
.body()method. This eliminates manual JSON creation and improves type safety.
Serialization Benefits
- No manual JSON writing
- Type-safe request payloads
- Cleaner code
- Easier maintenance
- Reusable request objects
What is Deserialization?
Deserialization is the reverse of serialization.
It converts a JSON response into a Java object (POJO).
This allows response values to be accessed using Java methods instead of manually parsing JSON.
Deserialization Flow
JSON Response
│
▼
Deserialization
│
▼
Java Object
│
▼
Use Getters
Sample JSON Response
{
"name":"John",
"email":"john@test.com",
"age":28
}
REST Assured Example
User user =
given()
.when()
.get("/users/101")
.then()
.extract()
.as(User.class);
Access Values
System.out.println(user.getName());
System.out.println(user.getEmail());
System.out.println(user.getAge());
Manual Deserialization Using Jackson
ObjectMapper mapper = new ObjectMapper();
User user =
mapper.readValue(responseBody, User.class);
Manual Deserialization Using Gson
Gson gson = new Gson();
User user =
gson.fromJson(responseBody, User.class);
Why Use Deserialization?
Benefits include:
- Type-safe response handling
- Easy access using getters
- Better readability
- Cleaner validation
- Simplified API chaining
Serialization vs Deserialization
| Serialization | Deserialization |
|---|---|
| Java Object → JSON | JSON → Java Object |
| Used for request payloads | Used for response handling |
| Sent to server | Received from server |
.body(user) |
.extract().as(User.class) |
Real-Time Example
After executing the Login API, I deserialized the JSON response into a
UserPOJO. Instead of extracting individual fields using JsonPath, I accessed them directly through getter methods such asgetName()andgetEmail(), making the code cleaner and more maintainable.
Jackson vs Gson
| Feature | Jackson | Gson |
|---|---|---|
| Developed By | FasterXML | |
| Performance | Faster | Good |
| Commonly Used With REST Assured | Yes | Yes |
| Serialization | ✔ | ✔ |
| Deserialization | ✔ | ✔ |
| Annotation Support | Extensive | Basic |
Best Practices
- Create one POJO class per API request or response model.
- Keep field names aligned with the JSON keys returned by the API.
- Include getters and setters for all fields.
- Use constructors where appropriate.
- Let REST Assured handle automatic serialization whenever possible.
- Use Jackson or Gson for custom serialization and deserialization scenarios.
- Reuse POJO classes across multiple test cases to improve maintainability.
Frequently Asked Questions (FAQs)
1. What is a POJO class?
A POJO (Plain Old Java Object) is a simple Java class containing private fields, getters, setters, and optionally constructors. In REST Assured, POJO classes are used to represent API request payloads and to map JSON responses into Java objects.
2. Why use POJO in REST Assured?
POJO classes provide:
- Type safety
- Reusable request and response models
- Easy Java-to-JSON and JSON-to-Java mapping
- Cleaner, more maintainable code
- Seamless integration with Jackson and Gson
3. What is serialization?
Serialization is the process of converting a Java object (POJO) into JSON so it can be sent as an API request payload.
REST Assured performs this automatically when a POJO is passed to .body() with the Content-Type set to application/json.
4. How do you serialize a Java object to JSON?
There are two common approaches:
Automatic serialization (REST Assured):
.body(user)
Manual serialization (Jackson):
ObjectMapper.writeValueAsString(user)
Manual serialization (Gson):
gson.toJson(user)
5. What is deserialization?
Deserialization is the process of converting a JSON response into a Java object (POJO). This allows response data to be accessed using Java getter methods instead of manually parsing JSON.
6. Which libraries handle serialization and deserialization?
The two most commonly used libraries are:
- Jackson (
ObjectMapper) - Gson
Both integrate seamlessly with REST Assured and support automatic as well as manual serialization and deserialization.