Last active
August 29, 2015 14:05
-
-
Save marcus-nl/b7c86062894b7c063c1f to your computer and use it in GitHub Desktop.
Jackson: Explicit Properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonFilter("explicitProperties") | |
public abstract class ExplicitPropertiesMixin {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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