java - 如何为 Spring Data Cassandra 创建自定义转换?
问题描述
我无法创建自定义转换以使用 Currency 和 Locale 作为数据类型。
我正在使用一个带@Configuration
注释的类,它将自动配置为META-INF/spring.factories
:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.acme.autoconfigure.ConverterAutoConfiguration
我尝试将转换器直接注册为 bean,并尝试创建CassandraCustomConversions
bean,但没有成功:
@Configuration
public class ConverterAutoConfiguration {
/*
@Bean
public Converter<String, Currency> currencyReadConverter() {
return new Converter<String, Currency>() {
@Override
public Currency convert(String source) {
return Currency.getInstance(source);
}
};
}
@Bean
public Converter<Currency, String> currencyWriteConverter() {
return new Converter<Currency, String>() {
@Override
public String convert(Currency source) {
return source.toString();
}
};
}
@Bean
public Converter<String, Locale> localeReadConverter() {
return new Converter<String, Locale>() {
@Override
public Locale convert(String source) {
return StringUtils.parseLocaleString(source);
}
};
}
@Bean
public Converter<Locale, String> localeWriteConverter() {
return new Converter<Locale, String>() {
@Override
public String convert(Locale source) {
return source.toString();
}
};
}
*/
@Bean
public CassandraCustomConversions cassandraCustomConversions() {
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(CurrencyReadConverter.INSTANCE);
converters.add(CurrencyWriteConverter.INSTANCE);
converters.add(LocaleReadConverter.INSTANCE);
converters.add(LocaleWriteConverter.INSTANCE);
return new CassandraCustomConversions(converters);
}
enum CurrencyReadConverter implements Converter<String, Currency> {
INSTANCE;
@Override
public Currency convert(String source) {
return Currency.getInstance(source);
}
}
enum CurrencyWriteConverter implements Converter<Currency, String> {
INSTANCE;
@Override
public String convert(Currency source) {
return source.toString();
}
}
enum LocaleReadConverter implements Converter<String, Locale> {
INSTANCE;
@Override
public Locale convert(String source) {
return StringUtils.parseLocaleString(source);
}
}
enum LocaleWriteConverter implements Converter<Locale, String> {
INSTANCE;
@Override
public String convert(Locale source) {
return source.toString();
}
}
}
使用CassandraCustomConversions
bean,我在启动时直接遇到异常:
Caused by: org.springframework.data.mapping.MappingException: Cannot resolve DataType for type [class java.lang.String] for property [categoryId] in entity [com.acme.model.Category]; Consider registering a Converter or annotating the property with @CassandraType.
它似乎失去了所有默认映射。
直接使用转换器 bean 时,出现以下异常:
org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract reactor.core.publisher.Flux com.acme.repository.CategoryLocaleReactiveRepository.findByUriNameInAndKeyLocale(java.util.Collection,java.util.Locale)! Reason: Could not inline literal of type java.util.Locale. This happens because the driver doesn't know how to map it to a CQL type. Try passing a TypeCodec or CodecRegistry to literal().
基于这个问题,这应该是可能的:https ://github.com/spring-projects/spring-boot/issues/8411
解决方案
尝试将@WritingConverter
和添加@ReadingConverter
到您的转换器。我相信 spring 在决定哪个转换器与非自定义类型一起使用时有点挣扎。
一旦将这两个注释添加到适当的转换器,我创建了一个本地项目并设法进行转换Locale
并工作。Currency
如果您仍有问题,我可以将我的测试项目推送到 Github 并分享。
参考:https ://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#customconversions.java
示例项目:https ://github.com/michaelmcfadyen/spring-data-cassandra-custom-converter-example
推荐阅读
- json - PostgreSQL json 的字符随 [] 变化
- reactjs - react build with gh-pages 生成错误的静态文件路径
- node.js - React中从父级到子级的回调函数
- mysql - 使用变量对 MySQL 表进行排序
- html - CSS翻转卡在过渡完成之前不显示背面相关子元素
- vue.js - 如何在没有任何错误的情况下将其他 JavaScript 文件连接到 Vue 文件?
- javascript - 如何在发出的 Highcharts 选择事件回调函数中获取角度组件类引用?
- r - 在R中用ggplot指定另一列的轴顺序
- python-3.x - 将具有依赖关系的python3脚本打包到exe中的最简单方法?
- node.js - 如何使用 node.js #nodejs 构建像 udemy 这样的视频播放器