The AliasFor
like support is not available in Jackson.But as an workaround we can modify the consumption of metadata supplied by annotation by extending JacksonAnnotationIntrospector
.
What you are trying to achieve can be done by providing an custom JacksonAnnotationIntrospector
which will supply the default implementation from the PolymorphismSupport
annotation.
@Inherited
@JacksonAnnotationsInside
@Retention(RetentionPolicy.RUNTIME)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public @interface PolymorphismSupport {
Class<?> defaultType() default Object.class;
}
public class CustomAnnotationIntrospector extends JacksonAnnotationIntrospector {
@Override
protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config, Annotated ann, JavaType baseType) {
TypeResolverBuilder<?> b = super._findTypeResolver(config, ann, baseType);
PolymorphismSupport support = _findAnnotation(ann, PolymorphismSupport.class);
if (null != b && null != support) {
b.defaultImpl(support.defaultType());
}
return b;
}
}
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
setAnnotationIntrospector(new CustomAnnotationIntrospector());
}
}
The only downside with this approach is that you have to register the introspector into the object mapper when initializing.