We're using @JacksonAnnotationsInside
and would like to inject a property from the classes using the meta annotation.
i.e. we have a meta annotation with @JsonTypeInfo()
and would like to inject the defaultImpl via the aggregating annotation.
Here is the annotation I'm trying to use:
@Inherited
@JacksonAnnotationsInside
@Retention(RetentionPolicy.RUNTIME)
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class") //, defaultImpl=defaultType())
public @interface PolymorphismSupport {
//@AliasFor("defaultImpl") ...
Class<?> defaultType() default Object.class;
}
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.
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