About this lesson
REST Assured: deserialize JSON to a POJO with as()
Learn REST AssuredAdvancedSerialization and Deserialization
How it works
Reading a response field by field with JsonPath works, but every path is a string the compiler cannot check. Deserialization maps the whole JSON object onto a Java class in one call, and from then on you use normal typed fields with IDE completion. In REST Assured the call is as() with the target class, available on a Response and on the result of extract().
Jackson does the mapping in this runtime and matches JSON keys to field names. Its default is strict in one direction and lenient in the other. A key in the JSON with no matching field throws an UnrecognizedPropertyException, which REST Assured wraps in a RuntimeException. A field in the class with no matching key is simply left at its default, null for objects and 0 for an int.
The class itself must be something Jackson can create. A nested class declared static, as in the starter, works. Drop the static keyword and the mapping fails with an InvalidDefinitionException (again wrapped in a RuntimeException), because an inner class needs an instance of the outer class that Jackson cannot supply.
About the assignment
Fetch user 1, convert the whole response into the provided User class, and print every field from the object.
What you will do
- GET
/users/1fromgiven(), optionally check status 200 withthen().statusCode(). - Call
extract().as()withUser.classto get aUserobject. - Print the five fields from the object after
ID:,Name:,Email:,Age:andCity:.
Common mistakes
Deserializing an error response
If the user does not exist, the body is an object with
errorandstatus. Mapping that ontoUserthrows becauseerroris an unknown field. Check the status first so the failure message is about the status, not about Jackson.Renaming a field to taste
Changing
emailtoemailAddressin the class means the JSON keyemailhas no home, and the whole call throws. Field names must equal the keys, or the mapping must be configured with annotations.Removing the static modifier
Making the nested class non-static looks harmless, but Jackson then cannot build it and the conversion fails. Keep model classes static or move them to their own files.
In a real test suite
Many teams mark response models to ignore unknown keys so that a new field added by the backend does not break every test. Others keep the strict default on purpose, treating any unexpected key as a contract change that someone should look at. Either way, the typed object makes assertions such as comparing two users far simpler than path strings.
Related lessons
- Send POJO as Request BodySerializes the same User shape into a request body.
- Deserialize to ArrayDeserializes the full users array instead of one object.
- Nested POJO DeserializationMaps a product with nested objects onto nested classes.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
public class Main {
static class User {
public int id;
public String name;
public String email;
public int age;
public String city;
}
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /users/1 and deserialize to User using .as(User.class)
// TODO: Print all fields from the User object
}
}