Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shihyuho/jackson-dynamic-filter
An easy way to determine filters dynamically using Jackson
https://github.com/shihyuho/jackson-dynamic-filter
filter gson jackson property spring-boot spring-mvc
Last synced: about 1 month ago
JSON representation
An easy way to determine filters dynamically using Jackson
- Host: GitHub
- URL: https://github.com/shihyuho/jackson-dynamic-filter
- Owner: shihyuho
- License: apache-2.0
- Created: 2016-12-15T04:17:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-31T02:52:45.000Z (over 6 years ago)
- Last Synced: 2024-09-29T08:41:01.597Z (about 2 months ago)
- Topics: filter, gson, jackson, property, spring-boot, spring-mvc
- Language: Java
- Homepage:
- Size: 37.1 KB
- Stars: 38
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/shihyuho/jackson-dynamic-filter.svg?branch=master)](https://travis-ci.org/shihyuho/jackson-dynamic-filter)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.shihyuho/jackson-dynamic-filter/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.shihyuho/jackson-dynamic-filter)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/shihyuho/jackson-dynamic-filter/blob/master/LICENSE)# Jackson Dynamic Property Filter
Basically, when you are using Gson and you need to exclude specific fields from Serialization WITHOUT annotations on the target object, you will use `ExclusionStrategy`. But I didn't find an similar way to do that in Jackson. So this repo provides an easy way to determine filters dynamically, and it also well integration with Spring MVC/Spring Boot.
> For Spring Boot: [jackson-dynamic-filter-spring-boot-starter](https://github.com/shihyuho/jackson-dynamic-filter-spring-boot-starter)
## Requirements
- Java 8
- Supported Spring IO Platform:
- [Athens-SR1](http://docs.spring.io/platform/docs/Athens-SR1/reference/htmlsingle/#appendix-dependency-versions)
- [Brussels-SR11](http://docs.spring.io/platform/docs/Brussels-SR11/reference/htmlsingle/#appendix-dependency-versions)
- [Cairo-SR2](http://docs.spring.io/platform/docs/Cairo-SR2/reference/htmlsingle/#appendix-dependency-versions)## Download
To add a dependency using Maven, use the following:
```xml
com.github.shihyuho
jackson-dynamic-filter
1.0.1```
To add a dependency using Gradle:
```xml
compile 'com.github.shihyuho:jackson-dynamic-filter:1.0.1'
```To download directly: [Releases](https://github.com/shihyuho/jackson-dynamic-filter/releases)
## Usage
```java
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(Object.class, DynamicFilterMixIn.class);
mapper.setFilterProvider(new DynamicFilterProvider());String jsonWithAllFields = mapper.writeValueAsString(someObject);
PropertyFilter someFilter = SimpleBeanPropertyFilter.serializeAllExcept("someField");
String jsonWithoutSomeField = mapper
.writer(new DynamicFilterProvider(someFilter)) // determine custom filter
.writeValueAsString(someObject);
```## Spring intergration
### Enabling in your Spring MVC
All you need to do is to wire `DynamicFilterResponseBodyAdvice` into your application. `DynamicFilterResponseBodyAdvice` implements Spring's`AbstractMappingJacksonResponseBodyAdvice` and can be plugged in as follows:
```java
@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {@Override
public void configureMessageConverters(List> converters) {
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(Object.class, DynamicFilterMixIn.class);
mapper.setFilterProvider(new DynamicFilterProvider());
converters.add(new MappingJackson2HttpMessageConverter(mapper));
}@Bean
public DynamicFilterResponseBodyAdvice dynamicFilterResponseBodyAdvice() {
return new DynamicFilterResponseBodyAdvice();
}
}
```If you're using Spring Boot, take a look on [jackson-dynamic-filter-spring-boot-starter](https://github.com/shihyuho/jackson-dynamic-filter-spring-boot-starter)
### Using annotation
- `@SerializeAllExcept` - Same as `SimpleBeanPropertyFilter.serializeAllExcept(...)`
- `@FilterOutAllExcept` - Same as `SimpleBeanPropertyFilter.filterOutAllExcept(...)````java
@RestController
public class SomeController {
@SerializeAllExcept({"someField", "anotherField"})
@RequestMapping(value = "/without/some-fields", method = RequestMethod.GET)
public SomeObject withoutSomeFields() {
return someObject;
}
@FilterOutAllExcept({"someField", "anotherField"})
@RequestMapping(value = "/only/some-fields", method = RequestMethod.GET)
public SomeObject onlySomeFields() {
return someObject;
}
}
```> [SimpleBeanPropertyFilter javadoc](https://fasterxml.github.io/jackson-databind/javadoc/2.3.0/com/fasterxml/jackson/databind/ser/impl/SimpleBeanPropertyFilter.html)
### Custom annotation
You can annotate a custom annotation:
```java
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface WithoutAuditingFields {
}
``````java
public class WithoutAuditingFieldsResolver extends DynamicFilterProvider {
@Override
public PropertyFilter apply(WithoutAuditingFields annotation) {
return SimpleBeanPropertyFilter.serializeAllExcept("id", "createdBy", "createdTime",
"modifiedBy", "modifiedTime");
}
}
```register into `DynamicFilterResponseBodyAdvice`
```java
@Bean
public DynamicFilterResponseBodyAdvice dynamicFilterResponseBodyAdvice() {
DynamicFilterResponseBodyAdvice advice = new DynamicFilterResponseBodyAdvice();
advice.addResolvers(new WithoutAuditingFieldsResolver());
return advice;
}
```and then use it for you controller as follows:
```java
@RestController
public class SomeController {
@WithoutAuditingFields
@RequestMapping(value = "/some-path", method = RequestMethod.GET)
public SomeObject getSomeObject() {
return someObject;
}
}
```