-
Couldn't load subscription status.
- Fork 114
Open
Description
This is not a solution for every use case, in large part because the Reflections scan can take some time, but it's a pattern that may be generally applicable enough to describe in the documentation:
- Define an annotation for the subtypes, and meta-annotate with
@JsonTypeName:
@JsonTypeName
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FoocorpJsonType {
}
- Use
@FoocorpJsonTypeon all subtypes to be registered with the mapper:
@FoocorpJsonType
public class ConcreteMessage extends AbstractMessage {}
- In a suitable location (such as a
@PostConstructmethod on a Spring configuration class, or wherever theObjectMapperis built), use theorg.reflections:reflectionspackage and this magic spell:
Reflections reflections = new Reflections(
ConfigurationBuilder.build("com.foocorp.base.package")
);
mapper.registerSubtypes(reflections.getTypesAnnotatedWith(FoocorpJsonType.class).toArray(new Class[0]));
This will scan the classpath (limited for performance) for types annotated with the custom annotation and register them all with the mapper so they can be deserialized when seen.
Metadata
Metadata
Assignees
Labels
No labels