Vur*_*too 8 spring-data-r2dbc r2dbc r2dbc-postgresql
如何将自定义转换器添加到 mu spring boot 应用程序?我的实体字段
@CreatedDate
@Column(value = "create_time")
private Instant createTime;
Run Code Online (Sandbox Code Playgroud)
我的转换器是
@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();
}
};
}
Run Code Online (Sandbox Code Playgroud)
我有一个例外
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]
........
Run Code Online (Sandbox Code Playgroud)
我该如何解决它,请帮助我!
Tir*_*res 12
您需要向 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);
}
Run Code Online (Sandbox Code Playgroud)
您不需要覆盖整个 R2dbcConfiguration。
尝试分别创建ReadingConverter和WritingConverter,并将它们注册到Config文件中。
public class DatabaseConfig extends AbstractR2dbcConfiguration {
//...
@Override
protected List<Object> getCustomConverters() {
return List.of(
new PostReadingConverter(),
new PostStatusWritingConverter()
);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里查看完整的代码。
对于 Spring Boot 应用程序,尝试覆盖默认的 beans,或创建一个R2dbcCustomConversions.
@Bean
public R2dbcCustomConversions r2dbcCustomConversions(ConnectionFactory connectionFactory, ObjectMapper objectMapper) {
var dialect = DialectResolver.getDialect(connectionFactory);
var converters = List.of(
...
);
return R2dbcCustomConversions.of(dialect, converters);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10908 次 |
| 最近记录: |