All Projects → shihyuho → jackson-dynamic-filter

shihyuho / jackson-dynamic-filter

Licence: Apache-2.0 license
An easy way to determine filters dynamically using Jackson

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to jackson-dynamic-filter

Yan
使用Maven构建,整合Dubbo+Zookeeper+SpringMVC+Spring+MyBatis+Redis支持分布式的高效率便捷开发框架
Stars: ✭ 293 (+737.14%)
Mutual labels:  jackson, spring-mvc
Jsonschema2pojo
Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
Stars: ✭ 5,633 (+15994.29%)
Mutual labels:  gson, jackson
Highdsa
2018年本科毕设项目,已更新所有开发和部署文档。基于Dubbo、SSM、Shiro、ELK、ActiveMQ、Redis等实现的一套高可用、高性能、高可扩展的分布式系统架构,实现可支持业务的基础公共服务,API使用Restful风格对外暴露。已经实现的包括:发送邮件服务、FastDFS文件存储服务、ELK实时日志查询服务、Redis缓存服务、Mybatis数据库、阿里短信推送、Goeasy消息推送、Druid监控、ActiveMQ消息队列、shiro权限认证、cas单点登录、权限配置web系统、移动端后台系统。持续更新中......
Stars: ✭ 385 (+1000%)
Mutual labels:  jackson, spring-mvc
Spring Petclinic Rest
REST version of the Spring Petclinic sample application
Stars: ✭ 257 (+634.29%)
Mutual labels:  jackson, spring-mvc
spring-rest-2-ts
spring rest 2 ts is typescript generator which produces data model and services in typescript based on Spring MVC annotations. It supports generation for Angular and React
Stars: ✭ 59 (+68.57%)
Mutual labels:  gson, jackson
SerializedNameGen
Auto add or remove json annotation plugin, such as gson SerializedName, fastjson JSONField, jackson JsonProperty. It also support java and kotlin file.
Stars: ✭ 19 (-45.71%)
Mutual labels:  gson, jackson
Immutables
Annotation processor to create immutable objects and builders. Feels like Guava's immutable collections but for regular value objects. JSON, Jackson, Gson, JAX-RS integrations included
Stars: ✭ 3,031 (+8560%)
Mutual labels:  gson, jackson
Easyjson
Provides an unified JSON access API, you can adapter any JSON library to Gson, Jackson, FastJson with easyjson。 提供了一个JSON门面库,就像slf4j一样。easyjson本身不做json的操作,完全依赖于底层实现库。可以直接使用Easyjson的API,底层的JSON库随时可切换。也可以使用其中某个json的API,然后通过easyjson适配给其他的json库
Stars: ✭ 54 (+54.29%)
Mutual labels:  gson, jackson
Botwall4j
A botwall for Java web applications
Stars: ✭ 41 (+17.14%)
Mutual labels:  filter, spring-mvc
Jsontokotlinclass
🚀 Plugin for Android Studio And IntelliJ Idea to generate Kotlin data class code from JSON text ( Json to Kotlin )
Stars: ✭ 2,438 (+6865.71%)
Mutual labels:  gson, jackson
methanol
⚗️ Lightweight HTTP extensions for Java
Stars: ✭ 172 (+391.43%)
Mutual labels:  gson, jackson
object.omit
Return a copy of an object without the given keys.
Stars: ✭ 79 (+125.71%)
Mutual labels:  filter, property
spring-boot-jpa-rest-demo-filter-paging-sorting
Spring Boot Data JPA with Filter, Pagination and Sorting
Stars: ✭ 70 (+100%)
Mutual labels:  filter, spring-mvc
kotlin-postgres
A Simple CRUD API using Spring Boot Application using PostgreSQL Database
Stars: ✭ 20 (-42.86%)
Mutual labels:  spring-mvc
Filmroom
A Image Processing test field of Apple Platform. Mainly using Swift and Metal
Stars: ✭ 42 (+20%)
Mutual labels:  filter
YaLafi
Yet another LaTeX filter
Stars: ✭ 50 (+42.86%)
Mutual labels:  filter
express-mquery
Expose mongoose query API through HTTP request.
Stars: ✭ 37 (+5.71%)
Mutual labels:  filter
go-tc
traffic control in pure go - it allows to read and alter queues, filters and classes
Stars: ✭ 245 (+600%)
Mutual labels:  filter
godsend
A simple and eloquent workflow for streaming messages to micro-services.
Stars: ✭ 15 (-57.14%)
Mutual labels:  filter
spring-filter
Painless filtering library for JPA entities and MongoDB collections. Smoothly integrates with Spring APIs.
Stars: ✭ 123 (+251.43%)
Mutual labels:  filter

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;
	}
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].