How can I add my custom converter to mu spring boot application? My entity field
@CreatedDate
@Column(value = "create_time")
private Instant createTime;
My converters are
@Bean
public Converter<Long, Instant> longInstantConverter() {
return new Converter<Long, Instant>() {
@Override
public Instant convert(Long source) {
return Instant.ofEpochMilli(source);
}
};
}
@Bean
public Converter<Instant, Long> instantLongConverter() {
return new Converter<Instant, Long>() {
@Override
public Long convert(@NotNull Instant source) {
return source.toEpochMilli();
}
};
}
I have an exception
org.springframework.data.mapping.MappingException: Could not read property @org.springframework.data.relational.core.mapping.Column(value=create_time) @org.springframework.data.annotation.CreatedDate()private java.time.Instant com.example.database.model.MyTable.createTime from result set!
.........
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Long] to type [java.time.Instant]
........
How can I fix it, please help me!
Try to create
ReadingConverter
andWritingConverter
respectively, and register them in the Config file.Check the complete codes here.
For Spring Boot applications, try to override the default beans, or create a
R2dbcCustomConversions
.