Skip to content

doyleyoung/dynafilter-proj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Dyna Filter

Dynamic Object filtering for Json serialization in Spring.

This library provides a non-intrusive way to dynamically filter fields for serialization.

Current Release

<dependency>
	<groupId>com.github.bmsantos</groupId>
	<artifactId>dynafilter</artifactId>
	<version>1.0.0</version>
</dependency>

How does it work?

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>

Lookup strategy

When looking for fields listed in the annotation, the following lookup strategy is used:

  1. Fields/attributes/properties
  2. Access methods (Camel cased methods that start with get) [1]
  3. Exact method (methods with the exact same name) [1]

[1] Methods must have no arguments

Usage

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.

Build project and run tests

mvn clean install

Manual test execution

cd web-test
mvn jetty:run

Then use the following links to see examples of:

  1. Simple instance with primitive types
  2. Collection of simple instances
  3. Collection of mixed simples instances
  4. Graph of complex instance
  5. Fully filtered object
  6. Fully filtered collection
  7. Include nulls
  8. Named filters
  9. Method access

About

Dynamic Object filtering for Json serialization in Spring MVC.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages