Table of contents
1.
Introduction
2.
Dynamic Filtering
3.
Implementation of Dynamic Filtering for RESTful Services
4.
Frequently Asked Questions
4.1.
What is the difference between static filtering and dynamic filtering?
4.2.
What is a RESTful service?
4.3.
What are Web API filters?
4.4.
What is paging in the REST API?
4.5.
Is REST API and RESTful the same?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Implementing Dynamic Filtering for RESTful Services

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Filtering is the process in which we apply a Boolean condition to a set of resources to narrow the set to only some resources. The condition is true only for these resources. 

If you have ever worked on SQL, filtering is similar to a SQL query using a WHERE clause.

introd

This article is focused on the implementation of Dynamic Filtering for RESTful Services.

Dynamic Filtering

Dynamic filtering is a type of filtering in which we implement different filters for different services.

Let's assume there are three fields available: the first one is "name," the second one is the phone, and the third one is salary. If we want to send two fields, one is the name, and the other one is the salary for the first service. And the name and the phone for the second service.

However, dynamic filtering has its limitations. Dynamic filtering cannot be set up directly in the bean. Where we are retrieving the values, filtering needs to be configured. We employ a class called MappingJacksonValue to implement dynamic filtering. The filter method definition may be found in the class definition.

Let’s see the implementation of the dynamic filtering for RESTful Services

Implementation of Dynamic Filtering for RESTful Services

Now, let’s check out an example in which we will send a name and salary for “/filtering” mapping.

  1. Open the Java file named FilteringController.java.
     
  2. Now, create a constructor of the class named MappingJacksonValue. Then pass a bean or someBean as a constructor argument. The goal is to create a mapping Jackson value for this specific bean.
     
  3. We must first create the filters before configuring them. Declare local variables filters of type FilterProvider to create a filter. FilterProvider is also a class, but it is abstract. The SingleFilterProvider method is only implemented once in it. Use the SimpleBeanPropertyFilter filter and String id, the two parameters of the addFilter() function.
     
  4. Call the static function filterOutAllExcept() of the SimpleBeanPropertyFilter class. The response is filtered to exclude the fields we've provided from the list of fields. We've supplied these two parameters because we want to transmit the name and salary data in the result.
     
  5. Now, we will configure the filters.
     
  6. We will return mapping instead of returning someBean.
     
  7. Now that we have received mapping as a return. We will have to change the return type to MappingJacksonValue.
     
  8. Open the SomeBean.java file, and then use the annotation @JsonFilter to define a filter. It is utilized in classes. It specifies the name of a filter that we use to remove properties during JSON serialization.
     

After that, let’s move on to dynamic filtering. Here, we will return the name and phone data for “/filtering-list” mapping.

  1. The first step is to change the return type to MappingJacksonValue.
     
  2. Now, we will create a list of SomeBean.
     
  3. It’s time to specify the field names that we want to send.
     
  4. Pass the list as the arguments in the MappingJasksonValue constructor.
     
  5. Finally, return mapping.
     

FilteringController.java

package com.codingninjas.server.main.filtering;
import java.util.List;    
import java.util.Arrays;  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.http.converter.json.MappingJacksonValue;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;   
import org.springframework.web.bind.annotation.RestController;   
import com.fasterxml.jackson.databind.ser.FilterProvider;  
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;  


@RestController  
public class FilteringController   
{  
    /*  Method will return a bean as response values to send name and salary.  */                                                            
    @RequestMapping("/filtering")  
    public MappingJacksonValue retrieveSomeBean()  
    {  
        SomeBean testBean=new SomeBean("Shadow", "8888888888","39000");  
        /* Calling static method named filterOutAllExcept() */  
        SimpleBeanPropertyFilter testfilter=SimpleBeanPropertyFilter.filterOutAllExcept("name", "salary");  
        /* Create a testfilter using FilterProvider class. */ 
        FilterProvider testfilters=new SimpleFilterProvider().addFilter("SomeBeanFilter",testfilter);  
         
        MappingJacksonValue testmapping = new MappingJacksonValue(testBean);  
        /* Configurating of testfilters. */  
        testmapping.setFilters(testfilters);  
        return testmapping;  
    }  


    @RequestMapping("/filtering-list")  
    public MappingJacksonValue retrieveListOfSomeBeans()  
    {  
        List<SomeBean> list=Arrays.asList(new SomeBean("Newbie", "7777777777","20000"), new SomeBean("Ramesh", "5555555555","34000"));  
        /* Calling the static method filterOutAllExcept() */  
        SimpleBeanPropertyFilter testfilter=SimpleBeanPropertyFilter.filterOutAllExcept("name", "phone");  
        FilterProvider testfilters=new SimpleFilterProvider().addFilter("SomeBeanFilter",testfilter);  
        MappingJacksonValue testmapping = new MappingJacksonValue(list);  
        testmapping.setFilters(testfilters);  
        return testmapping;  
    }  
}  
You can also try this code with Online Java Compiler
Run Code

 

Now, it's time to open the REST Client named Postman. Send a GET request using the URL “http://localhost:8080/filtering”. It will return the fields name and salary, as shown in the below image.

output1

 

Now, again send a GET request using the URL “http://localhost:8080/filtering-list”. It will return a list that fields name and phone in the result, as shown in the image below:

output2

Frequently Asked Questions

What is the difference between static filtering and dynamic filtering?

While static filters stay open or closed until the setting is manually altered, dynamic filters constantly open and close.

What is a RESTful service?

The REST architecture provides the foundation for the lightweight, scalable, and maintainable Restful Web Services.

What are Web API filters?

Filters are available in web APIs to add additional logic before or after the action method call occurs.

What is paging in the REST API?

The JSON result obtained from the REST API can be paginated in the paging process.

Is REST API and RESTful the same?

Regarding APIs, there are no distinctions between REST and RESTful. The set of constraints is called REST. A RESTful API is one that complies with certain limitations.

Conclusion

In this article, we have studied the implementation of dynamic filtering for RESTful Services in detail. 

We hope that this article has provided you with the help to enhance your knowledge regarding dynamic filtering for RESTful.

We hope that this article has provided you with the help to enhance your knowledge regarding RESTful Services and if you would like to learn more, check out our articles on Introduction to restful web service and Implementing hateoas for restful services.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass