Dynamic Object filtering for Json serialization in Spring.
This library provides a non-intrusive way to dynamically filter fields for serialization.
<dependency>
<groupId>com.github.bmsantos</groupId>
<artifactId>dynafilter</artifactId>
<version>1.0.0</version>
</dependency>
The @DynaFilter annotation is used to filter out the fields of interest from any data type and works in conjunction with the usual request handler method:
@RequestMapping(value = "collection", method = GET, produces = "application/json")
@DynaFilter(value = Data.class, fields = { "name", "description" }, includeNulls = true)
public @ResponseBody List<Data> returnCollection() {
// ...
}
When necessary, the annotation is processed by a Spring handler. This is similar to the mechanism use by an interceptor.
To filter multiple data types use @DynaFilters to wrap multiple @DynaFilter annotations.
@RequestMapping(value = "hybrid", method = GET, produces = "application/json")
@DynaFilters({
@DynaFilter(value = User.class, fields = "name"),
@DynaFilter(value = Address.class, fields = "id")
})
public @ResponseBody List<Object> returnHybrid() {
// ...
}
To use the same filter multiple times, use the @NamedDynaFilters annotation:
@RequestMapping(value = "named", method = GET, produces = "application/json")
@NamedDynaFilters(value = { "userAge", "addressOnly" })
public @ResponseBody List<Object> shouldUseNamedFitlers() {
// ...
}
and configure the named filters in the respective factory:
<bean class="com.github.bmsantos.dynafilter.DynaFilterFactory">
<property name="namedFilters">
<list>
<bean class="com.github.bmsantos.dynafilter.NamedDynaFilter">
<constructor-arg value="userAge" /> // Filter name
<constructor-arg value="com.github.bmsantos.dynafilter.controller.User" /> // Type
<constructor-arg value="true" /> // Include nulls
<constructor-arg value="id,age" /> // Fields
</bean>
<bean class="com.github.bmsantos.dynafilter.NamedDynaFilter">
<constructor-arg value="addressOnly" /> // Filter name
<constructor-arg value="com.github.bmsantos.dynafilter.controller.Address" /> // Type
<constructor-arg value="id,address" /> // Field
</bean>
</list>
</property>
</bean>
When looking for fields listed in the annotation, the following lookup strategy is used:
- Fields/attributes/properties
- Access methods (Camel cased methods that start with get) [1]
- Exact method (methods with the exact same name) [1]
[1] Methods must have no arguments
Add the DynaFilterFactory to your Spring MVC Context configuration XML or bean:
<bean class="com.github.bmsantos.dynafilter.DynaFilterFactory" />
or
@Configuration
public class AppConfig {
@Bean
public DynaFilterFactory DynaFilterFactory() {
return new DynaFilterFactory();
}
}
Annotate the controller or any other bean with one or more filter annotations as shown in the "How does it work?" section above. The provided test controller and Spring context files demonstrate how easy it is with a working example.
mvn clean install
cd web-test
mvn jetty:run
Then use the following links to see examples of: