Spring Boot Dynamic Filtering

Spring Boot Dynamic Filtering with Examples

In this article, I am going to discuss Spring Boot Dynamic Filtering with Examples. Please read our previous article where we discussed Spring Boot Static Filtering with Examples.

What is Dynamic Filtering in Spring Boot?

In the previous article, we implemented static filtering in Spring Boot. Let us now learn dynamic filtering. In dynamic filtering, we define different filters for different services, as per the requirement.

For example, assume there are three fields, name, email, and salary, and two REST Controllers. For one REST controller, all details need to be shown and for the other controller, the salary attribute is to be hidden. Hence, we will have to configure dynamic filtering in the REST controller class.

Implementing Dynamic Filtering using @JsonIgnore

From the previous project, remove the @JsonIgnore or @JsonIgnoreProperties annotations from the Employee.java file. As of now, there should be 3 files in the src/main/java/com/dotnet/staticfiltering directory:

  1. Employee.java
  2. FilteringController.java
  3. StaticfilteringApplication.java

Step 1: Modify the content of FilteringController.java as follows:

Implementing Dynamic Filtering using @JsonIgnore in Spring Boot

We have performed the following modifications:

  1. Imported the required packages
  2. Modified the GetMapping() request as shown. This function now executes when a client sends a GET request to the URL http://localhost:8080/filtering. Here, only employees’ name and email shall be displayed. The use of each line is:
    1. Line 18: A filter is defined to be implemented.
    2. Line 19: An object, of type MappingJacksonValue, is created. This object is constructed using an object of the Employee class.
    3. Line 20: The filter is attached to the MappingJacksonValue object.
    4. Line 21: The MappingJacksonValue value, along with the set filter, is returned to the client.

Step 2: Modify the Employee.java file. For the above modifications to take effect, the Employee.java file should also be modified:

Implementing Dynamic Filtering using @JsonIgnore in Spring Boot

We have performed the following modifications:

  1. Imported the required package.
  2. Added the @JsonFilter annotation to the class. The name of this filter must match the name set in Step 1.

Step 3: Compile and execute the application. Ensure compilation is successful.

Spring Boot Dynamic Filtering with Examples

Step 4: Send a GET request to the URL http://localhost:8080/filtering via Postman. You should see the following reply:

Spring Boot Dynamic Filtering with Examples

As can be seen, the salary field is not visible. Congratulations! You now know how to implement dynamic filtering in Spring Boot.

The Complete Example Code
Employee.java
package com.dotnet.staticfiltering;

import com.fasterxml.jackson.annotation.JsonFilter;

@JsonFilter("EmployeeFilter")
public class Employee
{
    private String name;
    private String email;
    private int salary;

    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 int getSalary()              {return salary;}
    public void setSalary(int salary)   {this.salary = salary;}
    public Employee()                   {}

    public Employee(String name, String email, int salary)
    {
        this.name = name;
        this.email = email;
        this.salary = salary;
    }
}
FilteringController.java
package com.dotnet.staticfiltering;

import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class FilteringController
{
    @GetMapping("/filtering")
    public MappingJacksonValue retreiveEmployee ()
    {
        FilterProvider filters = new SimpleFilterProvider().addFilter("EmployeeFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name","email"));
        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(new Employee("Employee 1", "employee1@company.com", 100000));
        mappingJacksonValue.setFilters(filters);
        return mappingJacksonValue;
    }    
}
StaticfilteringApplication.java
package com.dotnet.staticfiltering;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StaticfilteringApplication {

 public static void main(String[] args) {
  SpringApplication.run(StaticfilteringApplication.class, args);
 }
}

In the next article, I am going to discuss Spring Boot URI Versioning with Examples. Here, in this article, I try to explain Spring Boot Dynamic Filtering with Examples. I hope you enjoy this Spring Boot Dynamic Filtering article.

Leave a Reply

Your email address will not be published. Required fields are marked *