Skip to content

Instantly share code, notes, and snippets.

@marcus-nl
Last active August 29, 2015 14:05
Show Gist options
  • Save marcus-nl/b7c86062894b7c063c1f to your computer and use it in GitHub Desktop.
Save marcus-nl/b7c86062894b7c063c1f to your computer and use it in GitHub Desktop.
Jackson: Explicit Properties
public class ExplicitPropertiesFilter extends SimpleBeanPropertyFilter {
@Override
protected boolean include(BeanPropertyWriter writer) {
JsonProperty anno = writer.getAnnotation(JsonProperty.class);
return anno != null;
}
@Override
protected boolean include(PropertyWriter writer) {
if (writer instanceof BeanPropertyWriter) {
return include((BeanPropertyWriter) writer);
}
return true;
}
}
@JsonFilter("explicitProperties")
public abstract class ExplicitPropertiesMixin {}
@Configuration
public class JacksonConfig extends WebMvcConfigurerAdapter {
@Bean
public ObjectMapper jacksonObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixInAnnotations(Object.class, ExplicitPropertiesMixin.class);
objectMapper.setFilters(
new SimpleFilterProvider()
.addFilter("explicitProperties", new ExplicitPropertiesFilter())
);
return objectMapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment