I want to enable a custom jackson deserializer of some fields of type String. The deserializer also needs to be injected with a guice based dependency bean. SampleCode below
I haven't used Guice that much, but I guess it is somewhat similar to Spring. So I will post this answer here in case you can use the same principles as I have, and that someone using Spring reads this.
First I annotate my Jackson module as a Spring @Component:
@Component
public class FlowModule extends SimpleModule {
@Autowired private SupplierItemDeserializer supplierItemDeserializer;
This means I can get whatever I want @Autowired into my module. As you can see I have also done the same thing to my de- / serializers:
@Component
public class SupplierItemDeserializer extends JsonDeserializer {
@Autowired
private SupplierService supplierService;
I have then created a service that deals with converting to and from JSON:
@Service
public class IOService {
private ObjectMapper mapper;
@Autowired
private FlowModule flowModule;
@PostConstruct
public void initialize() {
mapper = new ObjectMapper();
mapper.registerModule(flowModule);
mapper.registerModule(new JavaTimeModule());
}
The service contains an ObjectMapper with my module that has all the right wiring.