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?
If you don't need to provide a custom JDBC binding and fetching logic, then the JPA AttributeConverter is a viable solution to define the mapping between a given Java Object type and a database column type.
Spring provides out-of-the-box various converters for built-in types; this means converting to/from basic types like String, Integer, Boolean and a number of other types. Apart from this, Spring also provides a solid type conversion SPI for developing our custom converters.
A converter converts a source object of type S to a target of type T . Implementations of this interface are thread-safe and can be shared.
The @Repository annotation can have a special role when it comes to converting database exceptions to Spring-based unchecked exceptions.
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;
}
.....
.....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With