jackson-dynamic-filter-spring-boot-starter

Spring Boot Starter for jackson-dynamic-filter

License

License

Categories

Categories

Spring Boot Container Microservices Jackson Data JSON
GroupId

GroupId

com.github.shihyuho
ArtifactId

ArtifactId

jackson-dynamic-filter-spring-boot-starter
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

jackson-dynamic-filter-spring-boot-starter
Spring Boot Starter for jackson-dynamic-filter
Project URL

Project URL

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

Source Code Management

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

Download jackson-dynamic-filter-spring-boot-starter

How to add to project

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

Dependencies

compile (4)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar
javax.validation : validation-api jar
com.github.shihyuho : jackson-dynamic-filter jar 1.0.1
org.slf4j : slf4j-api jar

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.0

test (4)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar
org.springframework.boot : spring-boot-starter-web jar
org.mockito : mockito-core jar
org.assertj : assertj-core jar 3.10.0

Project Modules

There are no modules declared in this project.

Build Status Maven Central License

Jackson Dynamic Property Filter - Spring Boot Starter

Spring Boot Starter for jackson-dynamic-filter, which provides an easy way to determine filters dynamically with jackson WITHOUT writing annotations directly on java object, and it also well integration with Spring MVC/Spring Boot.

Prerequisites

Setup

All you need to do is to add dependency in your Spring Boot Maven project:

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

Or in your Spring Boot Gradle project:

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

and it's done, have fun!

Using annotation

  • @SerializeAllExcept - Serializing all properties, except for ones explicitly listed to be filtered out.
  • @FilterOutAllExcept - Filtering out unknown properties and only serializes ones explicitly listed.
@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;
  }
}

Custom annotation

Define a custom annotation:

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface WithoutAuditingFields {
}

then register a resolver, extends DynamicFilterResolver, as Spring bean. Starter will auto find out all DynamicFilterResolver (including subclasses) in Spring context.

@Component
public class WithoutAuditingFieldsResolver extends DynamicFilterResolver<WithoutAuditingFields> {

  @Override
  public PropertyFilter apply(WithoutAuditingFields annotation) {
    return SimpleBeanPropertyFilter.serializeAllExcept(
        "id", "createdBy", "createdTime", "modifiedBy", "modifiedTime");
  }
}

More detail about SimpleBeanPropertyFilter

then use it for you controller as follows:

@RestController
public class SomeController {

  @WithoutAuditingFields
  @GetMapping(value = "/some-path")
  public SomeObject getSomeObject() {
    return someObject;
  }
}

Application properties

The following properties can be specified inside your application.properties file, inside your application.yml file, or as command line switches.

# Application will halt if any exception occurred while initialing jackson-dynamic-filter; otherwise just write a error log.
jackson.dynamic.filter.fail-fast=false 

# Resolver classes (with a default constructor) that are not Spring beans, but still need to inject into jackson-dynamic-filter
jackson.dynamic.filter.resolver.class-names=this.is.my.Resolver,yet.another.Resolver

Code Examples

Actual Spring Boot configurations and examples can be found in the ./_examples/ directory.

Versions

Version
1.0.1
1.0.0