Autowiring in JsonDeserializer: SpringBeanAutowiringSupport vs HandlerInstantiator

Adding to the Amir Jamak’s answer, you don’t have to create custom HandlerInstantiator as Spring has it already which is SpringHandlerInstantiator.

What you need to do is to hook it up to Jackson2ObjectMapperBuilder in Spring configuration.

@Bean
public HandlerInstantiator handlerInstantiator(ApplicationContext applicationContext) {
    return new SpringHandlerInstantiator(applicationContext.getAutowireCapableBeanFactory());
}

@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder(HandlerInstantiator handlerInstantiator) {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.handlerInstantiator(handlerInstantiator);
    return builder;
}

Leave a Comment