以编程方式设置 MongoDb 转换器

2022-09-01 18:13:42

我正在尝试将自定义转换器与spring-data-mongodb一起使用。我想以编程方式创建它,但我收到以下错误:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of     converting from type org.joda.time.LocalDate to type java.lang.String
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:475)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:154)
....
....

以下是失败的代码片段:

    Mongo mongo = new Mongo();
    MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongo, "database");

    List<Converter> converters = new ArrayList<>();
    converters.add(new LocalDateWriteConverter());
    converters.add(new LocalDateReadConverter());
    CustomConversions customConversions = new CustomConversions(converters);

    MappingContext mappingContext = new SimpleMongoMappingContext();
    MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(mongoDbFactory, mappingContext);
    mappingMongoConverter.setCustomConversions(customConversions);

    MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, mappingMongoConverter);

    MongoDbEvent mongoEvent = new MongoDbEvent(new LocalDate(2012, 12, 8));
    mongoTemplate.insert(mongoEvent);

这是我的转换器类:

class LocalDateReadConverter implements Converter<String, LocalDate> {
    @Override
    public LocalDate convert(String s) {
        // Conversion code omitted.
    }
}

class LocalDateWriteConverter implements Converter<LocalDate, String> {

    @Override
    public String convert(LocalDate localDate) {
        // Conversion code omitted.
    }
}

我试图坚持的类看起来像这样:

import org.joda.time.LocalDate;

public class MongoDbEvent {

    private String id;
    private LocalDate date;

    public MongoDbEvent(LocalDate date) {
        this.date = date;
    }

    public String getId() {
        return id;
    }

    public LocalDate getDate() {
        return date;
    }

    @Override
    public String toString() {
        return "MongoDbEvent{" +
                "id='" + id + '\'' +
                ", date=" + date +
                '}';
        }
}

答案 1

对于OP来说,这个答案可能有点晚了,但我今天遇到了同样的问题,并找到了解决方案......

若要以编程方式进行设置,需要在使用它之前进行调用。我从阅读 的代码中意识到了这一点。MongoMappingConverter.afterPropertiesSet()MongoTemplate.getDefaultMongoConverter(MongoDbFactory)

下面是一个示例:

MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory, context);
converter.setTypeMapper(mapper);
converter.setCustomConversions(new CustomConversions(
        Arrays.asList(
                new TimeZoneReadConverter(),
                new TimeZoneWriteConverter()
        )
));
converter.afterPropertiesSet();
MongoTemplate template = new MongoTemplate(mongoDbFactory, converter);

答案 2

只是一个抬头。我在使用Java配置时一直在努力解决这个问题。由于某些类已更改,因此我将发布我的解决方案。spring-data-mongodb 1.5.1.RELEASE

在配置类中添加以下定义,并标注为 :@Configuration

@Bean
public Mongo mongo() throws Exception {
    MongoPropertiesResolver resolver = mongoResolver();
    return new MongoClient(resolver.getUrl());
}

@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(mongo(), "database");
}

@Bean
public MongoTemplate mongoTemplate() throws Exception {
    return new MongoTemplate(mongoDbFactory(), mongoConverter());
}

@Bean
public CustomConversions customConversions() {
    List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
    converters.add(new TimeZoneReadConverter());
    converters.add(new TimeZoneReadConverter());
    return new CustomConversions(converters);
}

@Bean
public MappingMongoConverter mongoConverter() throws Exception {
    MongoMappingContext mappingContext = new MongoMappingContext();
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
    MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext);
    mongoConverter.setCustomConversions(customConversions());
    return mongoConverter;
}