如何为 Spring Data Cassandra 创建自定义转换?

How to create custom conversions for Spring Data Cassandra?

我无法创建自定义转换以将货币和区域设置用作数据类型。

我正在使用 @Configuration 注释 class,它将自动配置为 META-INF/spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.acme.autoconfigure.ConverterAutoConfiguration

我尝试将转换器直接注册为 beans,也尝试创建 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 在决定​​将哪个转换器用于非自定义类型时有点挣扎。

我创建了一个本地项目,并在将这 2 个注释添加到适当的转换器后设法使 LocaleCurrency 的转换工作。

我可以将我的测试项目推送到 Github,如果您仍有问题,请与我分享。

参考:https://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#customconversions.java

示例项目:https://github.com/michaelmcfadyen/spring-data-cassandra-custom-converter-example