I want to autowire a spring dependency into a jackson deserialization converter. E.g.,
import com.fasterxml.jackson.databind.util.StdConverter;
@Component
public class LookupConverter extends StdConverter<T, T> {
@Autowired
private Repository<T> repo;
@Override
public IsoCountry convert(T value) {
repo.findById(value.getId()).orElse(value);
}
}
I have tried using: SpringBeanAutowiringSupport
e.g.,
public LookupConverter() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
but get the following message
Current WebApplicationContext is not available for processing of LookupConverter: Make sure this class gets constructed in a Spring web application. Proceeding without injection.
I have tried injecting a SpringHandlerInstantiator
into the the ObjectMapper
ala this and this
@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;
}
This also does not work, seemingly because the SpringHandlerInstantiator
is not being used and my custom Converter
is not being instantiated by spring.
Any pointers to how this can be accomplished using Spring Boot 2.1.0 would be much appreciated.