Rest Assured – How to test JSON Request using Jackson API


This tutorial focuses on the testing of a REST API (with JSON payload). We will use Jackson API to serialize the request.

It is suggested to go through these tutorials to understand about creating a JSON Object Payload using POJO (Plain Old Java Object).

How to create JSON Object Payload using POJO – Jackson API

How to create JSON Array Payload using POJO – Jackson API

How to create Nested JSON Object using POJO – Jackson API

To start with, we need to add Jackson Maven Dependency to the POM. Always add the latest version of Jackson dependency to your project.


This dependency will also transitively add the following libraries to the classpath:

  1. jackson-annotations
  2. jackson-core

In the below example, let us assume that we need to create a new Employee (POST Request) .To start with, we need to create POJO class of the JSON payload (EmployeeDetails). This POJO class should contain the data members corresponding to the JSON nodes and their corresponding getter and setter methods.

public class EmployeeDetails {

	// private variables or data members of pojo class
	private String name;
	private double salary;
	private int age;

    // Getter and Setters
	public String getName() {
		return name;

	public void setName(String name) { = name;

	public double getSalary() {
		return salary;

	public void setSalary(double salary) {
		this.salary = salary;

	public int getAge() {
		return age;

	public void setAge(int age) {
		this.age = age;


Now that we have our POJO class we can start writing some REST Assured Serialization tests!

Let’s start with REST Assured Serialization with JSON. I want to send a POST request to my EmployeeDetails API that will add a new Employee to the database. I will send a POJO of the employee in the request body. This is what the code looks like in the test class:

	public void createEmployee() {

		// Create an object of POJO class
		EmployeeDetails emp = new EmployeeDetails();

		// Converting a Java class object to a JSON payload as string
		ObjectMapper mapper = new ObjectMapper();
		String employeePrettyJson = null;
		try {
			employeePrettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
		} catch (JsonProcessingException e) {

		// GIVEN

				// WHEN

				// THEN
				.then().assertThat().statusCode(200).body("", equalTo("Vibha"))
				.body("message", equalTo("Successfully! Record has been added.")).log().body();


If you want to see the structure of Request, then add the below in the test code.

ObjectMapper mapper = new ObjectMapper();
String employeePrettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);

REST Assured Serialization with Jackson handled all the serialization work for us. Great! See this has become so simple with the help of Jackson API.

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s