I have seen the many questions and answers about ignoring certain annotations, or even disabling all annotations:
.configure(MapperFeature.USE_ANNOTATIONS, false)
But
- the first solution defeats DefaultTyping (types don't end up in the serialized JSON and
- the second solution defeats many useful annotations, of which the most critical to us:
java.beans.ConstructorProperties
.
How can I ignore @JsonSerialize
and still have typeinfo in my resulting JSON while still supporting other annotations such as ConstructorProperties
?
Here's what I have so far:
private static ObjectMapper configureObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE, JsonTypeInfo.As.PROPERTY);
mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config, Annotated ann, JavaType baseType) {
if (ann.hasAnnotation(JsonSerialize.class) || ann.hasAnnotation(JsonDeserialize.class)) {
return StdTypeResolverBuilder.noTypeInfoBuilder(); // or null
}
return super._findTypeResolver(config, ann, baseType);
}
});
return mapper;
}
// or the same config using a JsonMapper builder
But this still processes @JsonSerialize
for some reason. I'm on jackson 2.10.0.pr3.
The real problem I'm facing is that I'm serializing 3rd party objects, which contain provided @JsonSerialize
for unrelated purposes, but without defining @JsonDeserialize
. Even though they're perfectly serializable without these annotations, they end up blocking our deserialization. At the same time I don't know upfront which classes they are, so these should be encoded in the resulting JSON. Furthermore, some of these objects are generated with Lombok resulting in no-arg constructors annotated with java.beans.ConstructorProperties
which Jackson can deal with fine under normal circumstances.
It's the above combination of configuration I'm not able to solve.