How to read RestAssured response into POJO class
- Dev Raj Sinha
- Jun 17, 2023
- 2 min read
To read a RestAssured response JSON into a POJO (Plain Old Java Object) class, you can leverage RestAssured's JSON parsing capabilities along with the use of popular libraries like Jackson or Gson for deserialization. Here's a step-by-step guide on how to achieve this:
Step 1: Define Your POJO Class
Start by creating a POJO class that represents the structure of the JSON response. This class should have fields that match the JSON key-value pairs. For example, let's consider a response that contains information about a user:
public class User {
private String name;
private int age;
// Include getters and setters for the fields
// ...
}
In this example, we define a `User` class with `name` and `age` fields.
Step 2: Send API Request and Receive Response
Next, send an API request using RestAssured and receive the response. Make sure the response body contains valid JSON data. Here's an example of sending a GET request and receiving a response:
import io.restassured.RestAssured;
import io.restassured.response.Response;
Response response = RestAssured.get("https://api.example.com/user/1");
In this example, we send a GET request to retrieve user data from the API, and the response is stored in the `response` variable.
Step 3: Deserialize JSON Response into POJO
To deserialize the JSON response into your POJO class, you can use a library like Jackson or Gson. Here, we'll demonstrate using Jackson for deserialization.
a. Add Jackson Dependency:
Include the Jackson dependency in your project's build configuration. You can add the Jackson dependency to your Maven or Gradle configuration, depending on your project setup.
For Maven, add the following dependency to your `pom.xml` file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
For Gradle, add the following dependency to your `build.gradle` file:
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
b. Deserialize JSON:
Once you have added the Jackson dependency, you can deserialize the JSON response into your POJO class. Here's an example:
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(response.getBody().asString(), User.class);
In this example, we use the `ObjectMapper` class from Jackson to deserialize the JSON response into an instance of the `User` class. We use the `readValue()` method, passing the response body as a string and the `User.class` as the target class.
Now, the `user` object contains the deserialized JSON response mapped to the `User` class.
Step 4: Access the Deserialized Data
You can now access the deserialized data from the `User` object as per your requirements. For example, you can access the name and age fields:
String userName = user.getName();
int userAge = user.getAge();
In this example, we retrieve the name and age from the `User` object using the getter methods.
By following these steps and leveraging libraries like Jackson or Gson, you can easily read a RestAssured response JSON into a POJO class, allowing you to work with the deserialized data in a structured and object-oriented manner.
Comments