Explain how to use filters.

Filters in REST Assured allow you to apply common functionality to your requests and responses. They can be used for logging, adding headers, or modifying the request and response.

Example :
import io.restassured.filter.Filter;
import io.restassured.filter.FilterContext;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
public class CustomFilter implements Filter {
    @Override
    public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
        // Add a custom header
        requestSpec.header("Custom-Header", "value");
        
        // Log the request
        System.out.println("Request: " + requestSpec.getMethod() + " " + requestSpec.getURI());
        
        // Proceed with the request
        Response response = ctx.next(requestSpec, responseSpec);
        
        // Log the response
        System.out.println("Response: " + response.getStatusCode());
        
        return response;
    }
}?

To use the custom filter in your tests :
import static io.restassured.RestAssured.given;
public class APITest {
    public void testAPI() {
        given()
            .filter(new CustomFilter())
            .when()
            .get("https://api.example.com/data")
            .then()
            .statusCode(200);
    }
}?