It's not possible to directly add a DeserializationProblemHandler
to the ObjectMapper
via a Jackson2ObjectMapperBuilder
or Jackson2ObjectMapperBuilderCustomizer
. The handlerInstanciator()
method is for something else.
However, it's possible to do so by registering a Jackson module:
- the builder has a
modules()
method
- the module has access via
setupModule()
to a SetupContext
instance, which has a addDeserializationProblemHandler()
method
This works:
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.modules(new MyModule());
}
};
}
private static class MyModule extends SimpleModule {
@Override
public void setupModule(SetupContext context) {
// Required, as documented in the Javadoc of SimpleModule
super.setupModule(context);
context.addDeserializationProblemHandler(new NullableFieldsDeserializationProblemHandler());
}
}