Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
Open the Java file named FilteringController.java.
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.
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.
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.
Now, we will configure the filters.
We will return mapping instead of returning someBean.
Now that we have received mapping as a return. We will have to change the return type to MappingJacksonValue.
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.
The first step is to change the return type to MappingJacksonValue.
Now, we will create a list of SomeBean.
It’s time to specify the field names that we want to send.
Pass the list as the arguments in the MappingJasksonValue constructor.
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
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.
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:
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.