Reputation: 3282
Friends,
Jackson framework provides annotation based approach to emit the type information during serialization process.
I do not want to use @JsonTypeInfo annotation in any of my class.
Is there any alternative/s to above annotation.
If yes, please provide example of to do the same if possible.
Upvotes: 4
Views: 2535
Reputation: 574
Override JacksonAnnotationIntrospector and use your Introspector in your ObjectMapper by using setAnnotationIntrospector. Here is a code snippet for your AnnotationIntrospector:
@Override
public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config,
AnnotatedClass ac, JavaType baseType) {
if (Modifier.isAbstract(baseType.getRawClass().getModifiers())){
StdTypeResolverBuilder typeResolverBuilder = new StdTypeResolverBuilder();
typeResolverBuilder.typeProperty("@class");
typeResolverBuilder.inclusion(As.PROPERTY);
typeResolverBuilder.init(Id.CLASS, null);
return typeResolverBuilder;
}
return super.findTypeResolver(config, ac, baseType);
}
Upvotes: 3
Reputation: 116472
Couple of alternatives:
JacksonAnnotationIntrospector
and implement your own logic for determining when and how equivalent type information should be used.Upvotes: 2