With Jackson, it's easy to disable all annotations for a given ObjectMapper
.
Is there a way to only disable one given annotation?
// disable all
ObjectMapper mapper = new ObjectMapper()
mapper.disable(MapperFeature.USE_ANNOTATIONS);
// disable one?
ObjectMapper mapper = new ObjectMapper()
mapper.disable(@JsonIgnore);
Using @JacksonAnnotationsInside
, I've defined a custom Jackson annotation and I only want it to be used in certain circumstances.
If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.
To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.
ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.
If you find that specific auto-configure classes are being applied that you don't want, you can use the exclude attribute of @EnableAutoConfiguration to disable them. If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.
Usually we need to disable the Jackson annotation after completing its work. So, we use the disable () method of ObjectMapper class for disabling the annotation. We pass the USE_ANNOTATIONS mapper feature to the disable () method of the ObjectMapper class.
You can configure the ObjectMapper to ignore annotations like @JsonProperty by doing: ObjectMapper objectMapper = new ObjectMapper ().configure (org.codehaus.jackson.map.DeserializationConfig.Feature.USE_ANNOTATIONS, false).configure (org.codehaus.jackson.map.SerializationConfig.Feature.USE_ANNOTATIONS, false)
We've covered the ObjectMapper class - the central API of Jackson for serialization and deserialization of Java Objects and JSON data. We've first taken a look at how to install Jackson, and then dived into converting JSON to Java Objects - from strings, files, HTTP Responses, InputStreams and byte arrays.
Jackson2ObjectMapperBuilderCustomizer The purpose of this functional interface is to allow us to create configuration beans. They will be applied to the default ObjectMapper created via Jackson2ObjectMapperBuilder: The configuration beans are applied in a specific order, which we can control using the @Order annotation.
This the best I've come across. I think I saw it on the Jackson user group forums somewhere.
Essentially it makes a custom annotation introspector, which returns null if it sees that it has a specific annotation (in this case JsonTypeInfo)
JacksonAnnotationIntrospector ignoreJsonTypeInfoIntrospector = new JacksonAnnotationIntrospector() {
@Override
protected TypeResolverBuilder<?> _findTypeResolver(
MapperConfig<?> config, Annotated ann, JavaType baseType) {
if (!ann.hasAnnotation(JsonTypeInfo.class)) {
return super._findTypeResolver(config, ann, baseType);
}
return null;
}
};
mapper.setAnnotationIntrospector(ignoreJsonTypeInfoIntrospector);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With