Search code examples
springjpaspring-bootjacksonspring-data-jpa

Autowiring into JPA converters


I am using a customized ObjectMapper in my spring boot app. I also use the JPA converters for several fields which are stored as JSON strings in the DB. I am not sure how to autowire my custom object mapper into my converter.

@Convert(converter=AddressConverter.class)
private Address address;

And my AddressConverter is

class AddressConverter implements AttributeConverter<Address, String> {

        @Autowire
        ObjectMapper objectMapper; //How to do this?
        .....
        .....
   }

How to autowire ObjectMapper into AddressConverter? Is there a way to do this with Spring AOP?


Solution

  • Maybe you can do it by changing it to a static property, like this:

    @Component
    class AddressConverter implements AttributeConverter<Address, String> {
    
        private static ObjectMapper objectMapper; 
    
        @Autowired
        public void setObjectMapper(ObjectMapper objectMapper){
            AddressConverter.objectMapper = objectMapper;
        }
        .....
        .....
    }