REST Assured – @JsonIgnoreProperties in Jackson

When working with REST APIs, handling JSON serialization and deserialization is a crucial part of testing. Jackson, a popular library in Java, makes this process seamless. However, there are times when we encounter unwanted fields in a JSON response or when we don’t want certain fields to be serialized while sending requests. This is where @JsonIgnoreProperties comes into play.

In this blog post, we’ll explore how to use @JsonIgnoreProperties in Jackson to manage such scenarios effectively with REST Assured.


What is @JsonIgnoreProperties?

The @JsonIgnoreProperties annotation is used to specify a list of fields to ignore during serialization or deserialization. This means:

  • Fields listed under this annotation will be excluded when converting a Java object to JSON (serialization).
  • Similarly, those fields will be ignored when mapping a JSON response to a Java object (deserialization).

Why Use @JsonIgnoreProperties in REST Assured?

When testing REST APIs, you might face challenges like:

  1. Ignoring additional or unexpected fields in a response payload.
  2. Avoiding sensitive fields from being serialized in request payloads.
  3. Simplifying your POJO classes by excluding irrelevant or dynamic fields.

@JsonIgnoreProperties simplifies managing such scenarios by eliminating the need for custom serializers or filtering logic.


How to Use @JsonIgnoreProperties?

1. Add the Annotation to Your POJO

To use @JsonIgnoreProperties, annotate your POJO class with it and specify the field names you want to ignore.

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@JsonIgnoreProperties({"fieldToIgnore", "anotherFieldToIgnore"})

public class User {

    private String name;

    private String email;

    private String password; // Sensitive field

    

    // Getters and Setters

    public String getName() {

        return name;

    }


    public void setName(String name) {

        this.name = name;

    }


    public String getEmail() {

        return email;

    }


    public void setEmail(String email) {

        this.email = email;

    }


    public String getPassword() {

        return password;

    }


    public void setPassword(String password) {

        this.password = password;

    }

}

In the above example, fields listed in the @JsonIgnoreProperties annotation (e.g., fieldToIgnore and anotherFieldToIgnore) will be ignored during serialization and deserialization.


2. Integrate with REST Assured

Let’s see how this works with a REST Assured test.

Example: Ignoring Extra Fields in a Response

Imagine a scenario where the API response includes additional metadata that you don’t want to handle in your test:

API Response:

{

  "name": "John Doe",

  "email": "john.doe@example.com",

  "extraField": "value not needed"

}

User POJO (with @JsonIgnoreProperties):

@JsonIgnoreProperties({"extraField"})
public class User {
    private String name;
    private String email;

    // Getters and Setters
}

REST Assured Test:

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

import io.restassured.response.Response;
import org.junit.jupiter.api.Test;

public class ApiTest {
    
    @Test
    public void testIgnoreExtraFields() {
        Response response = given()
            .when()
            .get("https://api.example.com/users/1")
            .then()
            .statusCode(200)
            .extract()
            .response();
        
        // Deserialize response to User object
        User user = response.as(User.class);
        
        // Assertions
        assertEquals("John Doe", user.getName());
        assertEquals("john.doe@example.com", user.getEmail());
    }
}

Here, the extraField from the API response is ignored during deserialization.


3. Avoid Serializing Sensitive Data

If you’re sending data to an API and don’t want certain fields (e.g., password) to be included in the request payload:

User POJO:

@JsonIgnoreProperties({"password"})

public class User {

    private String name;

    private String email;

    private String password;


    // Getters and Setters

}

REST Assured Test:

@Test
public void testIgnoreSensitiveFields() {
    User user = new User();
    user.setName("Jane Doe");
    user.setEmail("jane.doe@example.com");
    user.setPassword("securePassword123"); // This field will be ignored

    given()
        .contentType("application/json")
        .body(user) // Serializes the object to JSON
        .when()
        .post("https://api.example.com/users")
        .then()
        .statusCode(201);
}

In this example, the password field will not be included in the serialized JSON payload sent to the API.


Key Points to Remember

  • The fields specified in @JsonIgnoreProperties will not cause errors if they’re absent in the JSON payload.
  • You can use wildcards to ignore unknown properties: @JsonIgnoreProperties(ignoreUnknown = true).
  • This annotation is highly useful when dealing with APIs that may evolve or return dynamic fields.

Followers