jackson-dynamic-filter

The simplest way to specific filters dynamically using Jackson

License

License

Categories

Categories

Jackson Data JSON
GroupId

GroupId

com.github.shihyuho
ArtifactId

ArtifactId

jackson-dynamic-filter
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

jackson-dynamic-filter
The simplest way to specific filters dynamically using Jackson
Project URL

Project URL

http://github.com/shihyuho
Source Code Management

Source Code Management

https://github.com/shihyuho/jackson-dynamic-filter.git

Download jackson-dynamic-filter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.shihyuho/jackson-dynamic-filter/ -->
<dependency>
    <groupId>com.github.shihyuho</groupId>
    <artifactId>jackson-dynamic-filter</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.shihyuho/jackson-dynamic-filter/
implementation 'com.github.shihyuho:jackson-dynamic-filter:1.0.1'
// https://jarcasting.com/artifacts/com.github.shihyuho/jackson-dynamic-filter/
implementation ("com.github.shihyuho:jackson-dynamic-filter:1.0.1")
'com.github.shihyuho:jackson-dynamic-filter:jar:1.0.1'
<dependency org="com.github.shihyuho" name="jackson-dynamic-filter" rev="1.0.1">
  <artifact name="jackson-dynamic-filter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.shihyuho', module='jackson-dynamic-filter', version='1.0.1')
)
libraryDependencies += "com.github.shihyuho" % "jackson-dynamic-filter" % "1.0.1"
[com.github.shihyuho/jackson-dynamic-filter "1.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar
org.springframework : spring-webmvc jar

test (7)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar
org.springframework : spring-test jar
junit : junit jar
org.assertj : assertj-core jar 3.10.0
com.jayway.jsonpath : json-path jar
ch.qos.logback : logback-classic jar
org.slf4j : jcl-over-slf4j jar

Project Modules

There are no modules declared in this project.

Build Status Maven Central 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

Requirements

Download

To add a dependency using Maven, use the following:

<dependency>
	<groupId>com.github.shihyuho</groupId>
	<artifactId>jackson-dynamic-filter</artifactId>
	<version>1.0.1</version>
</dependency>

To add a dependency using Gradle:

compile 'com.github.shihyuho:jackson-dynamic-filter:1.0.1'

To download directly: Releases

Usage

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'sAbstractMappingJacksonResponseBodyAdvice and can be plugged in as follows:

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> 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

Using annotation

  • @SerializeAllExcept - Same as SimpleBeanPropertyFilter.serializeAllExcept(...)
  • @FilterOutAllExcept - Same as SimpleBeanPropertyFilter.filterOutAllExcept(...)
@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

Custom annotation

You can annotate a custom annotation:

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface WithoutAuditingFields {
}
public class WithoutAuditingFieldsResolver extends DynamicFilterProvider<WithoutAuditingFields> {
	@Override
	public PropertyFilter apply(WithoutAuditingFields annotation) {
		return SimpleBeanPropertyFilter.serializeAllExcept("id", "createdBy", "createdTime",
			"modifiedBy", "modifiedTime");
	}
}

register into DynamicFilterResponseBodyAdvice

@Bean
public DynamicFilterResponseBodyAdvice dynamicFilterResponseBodyAdvice() {
	DynamicFilterResponseBodyAdvice advice = new DynamicFilterResponseBodyAdvice();
	advice.addResolvers(new WithoutAuditingFieldsResolver());
	return advice;
}

and then use it for you controller as follows:

@RestController
public class SomeController {
  
	@WithoutAuditingFields
	@RequestMapping(value = "/some-path", method = RequestMethod.GET)
	public SomeObject getSomeObject() {
		return someObject;
	}
}

Versions

Version
1.0.1
1.0