首页 > 解决方案 > 如何为 Spring Data Cassandra 创建自定义转换?

问题描述

我无法创建自定义转换以使用 Currency 和 Locale 作为数据类型。

我正在使用一个带@Configuration注释的类,它将自动配置为META-INF/spring.factories

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

我尝试将转换器直接注册为 bean,并尝试创建CassandraCustomConversionsbean,但没有成功:

@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();
        }
    }
}

使用CassandraCustomConversionsbean,我在启动时直接遇到异常:

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

标签: javaspringspring-bootspring-dataspring-data-cassandra

解决方案


尝试将@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


推荐阅读