I have seen the many questions and answers about ignoring certain annotations , or even disabling all annotations:
.configure(MapperFeature.USE_ANNOTATIONS, false)
But
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.
Have you tried to override JacksonAnnotationIntrospector#findSerializer
method:
class SkipSerializersJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {
private final List<Class> classesToSkip = new ArrayList<>();
public SkipSerializersJacksonAnnotationIntrospector() {
classesToSkip.add(YourClass.class);
}
@Override
public Object findSerializer(Annotated a) {
if (classesToSkip.contains(a.getRawType())) {
return null;
}
return super.findSerializer(a);
}
}
It should allow you to skip JsonSerialize
annotation and keep type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.