A common way to customize JSON representations with Jackson is to register mixin types. The currently most convenient way is to declare a Module implementation that the calls setMixInAnnotation(…) on the module. The module itself can be exposed as a bean and will be picked up by Boot. That registration code is quite a bit of boilerplate to be written and could be simplified as follows:
- Introduce an annotation
@JsonMixin(Foo.class)that would be added to the mixin type.Foois the target type the mixin is registered for. - Boot could find classes annotated with
@JsonMixinand register a module that registers all mixin types declared like this for the type declared in the annotation.
I.e. a plain declaration of this:
@JsonMixin(MyType.class)
abstract class MyMixin { /* … */ }
would replace
@Configuration
class MyConfiguration {
@Bean
Module myModule() {
return new MixinModule(Map.of(MyType.class, MyMixin.class));
}
}
class MixinModule extends SimpleModule {
MixinModule(Map<Class<?>, Class<?>> mixins) {
mixins.entrySet().forEach(it -> setMixInAnnotation(it.getKey(), it.getValue()));
}
}
Comment From: philwebb
This would align quite nicely with the existing @JsonComponent support.
Comment From: wilkinsona
Thanks very much for the offer, @Tharik67. Please go ahead and let us know if you have any questions. It's a little too late for new features in next month's 2.6 release, but if you were able to open a PR we'd hope to include it in 2.7.
Comment From: wilkinsona
Closing in favor of #30152.