Spring Boot Static Filtering

Spring Boot Static Filtering with Examples

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

What is Static Filtering in Spring Boot?

Filters are a crucial feature provided by the JAX-RS framework. They can be utilized in a variety of situations and can be applied to either a request to a resource or the response from a resource, or both. For instance, imagine a scenario where we want to exclude certain class members from the response. This process is known as filtering. Jackson provides two annotations that can be used for filtering: @JsonIgnore and @JsonIgnoreProperties. These annotations allow developers to easily control which class members are included in the response.

Prepare an application for Static Filtering

Step 1: Create a new project using Spring Initializr in VS Code. Remember to include the Spring Web dependency. Ensure that the dependency has been installed correctly:

Prepare an application for Static Filtering

Step 2: Create a new java file called Employee.java in the src/main/java/com/dotnet/staticfiltering directory. This class will act as a POJO class for the controller.

Step 3: Add the following fields to the class:

What is Static Filtering in Spring Boot?

Step 4: Add setters, getters, a default constructor, and a normal constructor using the “Source Action…” option in the right-click menu.

Spring Boot Static Filtering with Examples

Step 5: Add a REST controller class called FilterController.java in the src/main/java/com/dotnet/staticfiltering directory.

Step 6: Modify the file as follows:

Spring Boot Static Filtering with Examples

We have performed the following modifications:

  1. Imported the required packages
  2. Added the @RestController annotation to the class. This marks it as a REST Controller.
  3. Added a function that executes when a GET request is sent to the http://localhost:8080/employees URL. This function creates a new object of type Employee and returns it to the client.

Note that up until this point, we have not added any static filtering. Let us see what happens if a GET request is sent to the application. Compile and execute the application. Send a GET request via Postman:

Spring Boot Static Filtering with Examples

As can be seen, all the employee information is sent to the client. Let us try to hide the salary attribute now. For this, we can use either the @JsonIgnore annotation or the @JsonIgnoreProperties annotation.

Implementing Static Filtering using @JsonIgnore

@JsonIgnore is an annotation that can be applied at the method or attribute level. It allows developers to exclude specific properties from the serialization and deserialization process by marking them individually.

For example, if we want to exclude a particular member from being included in the serialized output or from being populated during deserialization, we can annotate it (or its setter/getter) with @JsonIgnore. This gives developers fine-grained control over which properties are included in the serialization and deserialization process.

Step 1: Modify the code in Employee.java as follows:

Implementing Static Filtering using @JsonIgnore

We have performed the following modifications:

  1. Imported the required package.
  2. Added the @JsonIgnore annotation to the salary attribute.

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

Implementing Static Filtering using @JsonIgnore

Step 3: Send a GET request to the URL http://localhost:8080/employees. You should receive the following response:

Implementing Static Filtering using @JsonIgnore

As can be seen, the salary field is no longer visible. Congratulations! You now know how to implement a static filter using @JsonIgnore in Spring Boot.

The Complete Example Code (using @JsonIgnore)
Employee.java
package com.dotnet.staticfiltering;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Employee
{
    private String name;
    private String email;

    @JsonIgnore
    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 org.springframework.web.bind.annotation.GetMapping;

@RestController
public class FilteringController
{
    @GetMapping(value="/employees")
    public Employee getEmployee()
    {
        return new Employee("Employee 1", "employee1@company.com", 100000);

    }
}
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);
 }
}
Implementing Static Filtering using @JsonIgnoreProperties

@JsonIgnoreProperties is a class-level annotation. It ignores the logical properties in JSON serialization and deserialization. These properties will take part in the JSON serialization and deserialization. If the property salary is annotated with @JsonIgnore, then all the properties are 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.

Step 1: Modify the code in Employee.java as follows:

Implementing Static Filtering using @JsonIgnoreProperties

We have performed the following modifications:

  1. Imported the required package.
  2. Added the @JsonIgnoreProperties annotation to the class. Specified the attribute that needs to be hidden.

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

Implementing Static Filtering using @JsonIgnoreProperties

Step 3: Send a GET request to the URL http://localhost:8080/employees. You should receive the following response:

As can be seen, the salary field is no longer visible. Congratulations! You now know how to implement static filters using @JsonIgnoreProperties in Spring Boot.

The Complete Example Code (using @JsonIgnoreProperties)
Employee.java
package com.dotnet.staticfiltering;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties("salary")
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 org.springframework.web.bind.annotation.GetMapping;

@RestController
public class FilteringController
{
    @GetMapping(value="/employees")
    public Employee getEmployee()
    {
        return new Employee("Employee 1", "employee1@company.com", 100000);

    } 
}
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 Dynamic Filtering with Examples. Here, in this article, I try to explain Spring Boot Static Filtering with Examples. I hope you enjoy this Spring Boot Static Filtering article.

Leave a Reply

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