Implementing Static Filtering for RESTful Services
In this section, we will learn how to filter the request.
Filters are one of the important features provided by the JAX-RS framework. It is used in various contexts. It may be applied on either request to a resource or the response from a resource, or both.
Consider a scenario in which we do not want to show some class members in the response. This process is called filtering. Jackson has two annotations that are used in filtering are: @JsonIgnore and @JsonIgnoreProperties.
It is a member or method level annotation. It expects that the properties to be excluded are marked one by one. If we want to eliminate a member from the process of serialization and deserialization, we can annotate the actual property or its setter or getter.
Let's create a filter that filters the response. We will not touch the user example, but instead of this, we will create a new controller and bean to perform filtering.
Step 1: Create a Controller class with the name FilteringController.java in the package com.javatpoint.server.main.filtering.
Step 2: Create a bean with the name SomeBean.
Step 3: Create a class with the name SomeBean.java. Define three attributes name, phone, and salary.
Step 4: Generate Constructors.
Step 5: Generate Getters and Setters.
Step 6: Open the REST client Postman and send the GET request. It returns two fields: name and phone. The field salary will not send with the response.
The field salary will not be sent in the response whether we are sending one SomeBean as a response, or a list of SomeBeans as a response.
Step 7: Create another bean that returns a list of SomeBean.
Step 8: Send a GET request again with the URI http://localhost/filtering-list that returns a list of SomeBeans.
There is another approach to use an annotation @JsonIgnoreProperties.
@JsonIgnoreProperties is a class-level annotation. It ignores the logical properties in JSON serialization and deserialization.
In the following SomeBean.java file, we have specified the property name and phone, which we want to ignore in the response. These two properties will take part in the JSON serialization and deserialization.
If the property salary is annotated with @JsonIgnore, then all the properties ignored in the JSON serialization and deserialization.
In other words, the union of logical properties ignored by @JsonIgnore and @JsonIgnoreProperties annotation is considered to be ignored in JSON serialization and deserialization.
When we fire a GET request, it returns an empty list because the properties name and phone are specified in the @JsonIgnoreProperties and the property salary annotated with @JsonIgnore. Hence it returns the empty list.
Now remove the annotation @JsonIgnore and again fire a GET request. It returns only the salary property.
Whatever we have done is called static filtering. Suppose we want to ignore the name in one scenario and the salary in another scenario, we cannot do that using the static filtering. To implement this type of filtering, we use dynamic filtering.