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 this error?
You need to register the converters with R2DBC:
@Bean
public R2dbcCustomConversions customConversions() {
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(new SomeReadConverter());
converters.add(new SomeWriteConverter());
return R2dbcCustomConversions.of(MySqlDialect.INSTANCE, converters);
// deprecated: return new R2dbcCustomConversions(converters);
}
You don't need to override the whole R2dbcConfiguration.