【发布时间】:2020-12-02 10:34:29
【问题描述】:
我无法创建自定义转换来使用货币和区域设置作为数据类型。
我正在使用 @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
【问题讨论】:
标签: java spring spring-boot spring-data spring-data-cassandra