Skip to content
CodeKruPro

Deserialize Response to POJO

45/62

Deserialize Response to POJO

Use .as(ClassName.class) to convert a JSON response directly into a Java object:

Product product = given().get("/products/1").then().extract().as(Product.class);

The JSON fields are mapped to Java fields by name. This is the cleanest way to work with API responses in tests.

REST Assured uses Jackson here, which is strict: every key in the JSON needs a matching field in the class, or as() throws an UnrecognizedPropertyException. That is why the starter's User class declares all five fields.

Task: Deserialize user 1 into a POJO and print all fields.

Expected output:

ID: 1
Name: John Doe
Email: john@example.com
Age: 30
City: New York

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

  1. GET /users/1 from given(), optionally check status 200 with then().statusCode().
  2. Call extract().as() with User.class to get a User object.
  3. Print the five fields from the object after ID:, Name:, Email:, Age: and City:.

Common mistakes

  • Deserializing an error response

    If the user does not exist, the body is an object with error and status. Mapping that onto User throws because error is an unknown field. Check the status first so the failure message is about the status, not about Jackson.

  • Renaming a field to taste

    Changing email to emailAddress in the class means the JSON key email has 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.

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.

Main.java
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
    }
}