About this lesson
REST Assured POJO request body: serialization of a Java object to JSON
Learn REST AssuredAdvancedSerialization and Deserialization
How it works
Writing JSON by hand inside Java strings is fragile: one missing quote or escaped character and the payload is broken. REST Assured can instead take a plain Java object for body() and serialize it for you. In this runtime it uses Jackson, which writes every public field as a JSON key named exactly like the field.
Unset fields are still written. A String you never assigned becomes null in the JSON, and an int becomes 0. For testkru POST /users, which requires name and email, a forgotten email therefore leads to a 400 with an error naming the missing field, while a forgotten city is accepted and echoed back as null.
The content type decides whether the server reads the body at all. Without contentType(ContentType.JSON), REST Assured still writes the object as JSON text but labels the request as text/plain with an ISO-8859-1 charset. This API only parses bodies marked as JSON, so it sees no fields and answers 400 for both name and email. With the JSON content type set, the user is accepted with status 201 and returned with an id added.
About the assignment
Fill a User object with the values from the starter comment, send it as the body of a POST, and report the status plus the name and city the server returned.
What you will do
- Create a
Userwithnew User()and assign all four public fields: name, email, age and city. - Start from
given(), setcontentType()toContentType.JSON, and pass the object tobody(). - POST to
/usersand keep the response. - Print the status code after
Status:, then the returnednameandcityafterName:andCity:.
Common mistakes
Leaving out the content type
The object is serialized, but the request goes out as plain text, the server ignores it and the reply is 400. Set the JSON content type whenever you send an object.
Making the fields private without getters
Jackson writes public fields and properties exposed through getters. A class with only private fields and no getters gives it no properties at all, and serialization fails with an
InvalidDefinitionExceptionsaying no serializer was found. Keep the fields public as in the starter, or add getters.Using a primitive for an optional number
An
intfield can never be absent: an unset age is sent as 0, which an API may store as a real age. UseIntegerwhen a value is optional so that it serializes as null instead.
In a real test suite
Real suites keep request models in their own package, often with a builder or factory that fills sensible defaults so each test only sets the field it is about. The same classes double as response models, so a test can send an object and then compare what came back field by field.
Related lessons
- Create a User with POSTCreates a user with a JSON string body instead of an object.
- Deserialize Response to POJOGoes the other way, turning a JSON response into a POJO.
- Handle Validation ErrorsReads the list of validation errors a POST gets back when required fields are missing.
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 io.restassured.http.ContentType;
import static io.restassured.RestAssured.*;
public class Main {
static class User {
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: Create a User object with:
// name = "Test User", email = "test@example.com", age = 28, city = "Mumbai"
// TODO: POST to /users with the User object as the body
// TODO: Print status, name, and city from the response
}
}