Skip to content
CodeKruPro

Deserialize a response into a POJO

Automation taskMediumJava onlyAsked in: Accenture, Capgemini

"Have you used POJOs in API tests? How do serialization and deserialization work?" A POJO is a plain Java class that only holds data. Deserialization turns JSON text into such an object, and serialization turns an object into JSON text. Practise as(User.class) here: it turns a JSON response into a plain Java object you can assert on.

Deserialize Response to POJO

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