MongoDB没有找到能够从类型[java.lang.String]转换为类型[java.time.LocalDateTime]的转换器

问题描述 投票:3回答:2

MongoDB,Spring Data,findAll()方法错误:

没有找到能够从类型[java.lang.String]转换为类型[java.time.LocalDateTime]的转换器

public class EntityName {
    @Id
    private String id;
    private Map<LocalDateTime, Integer> statistic;
}

我能够保存实体,但无法加载它。任何快速修复?

java mongodb spring-data spring-data-mongodb
2个回答
2
投票

这解决了这个问题:

@Configuration
public class MongoConfiguration extends AbstractMongoConfiguration {

    @Value("${spring.data.mongodb.database:test}")
    private String database;

    @Value("${spring.data.mongodb.host:localhost}:${spring.data.mongodb.port:27017}")
    private String host;

    @Autowired
    private MappingMongoConverter mongoConverter;

    // Converts . into a mongo friendly char
    @PostConstruct
    public void setUpMongoEscapeCharacterConversion() {
        mongoConverter.setMapKeyDotReplacement("_");
    }

    @Override
    protected String getDatabaseName() {
        return database;
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient(host);
    }

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

    private static final class MongoLocalDateTimeFromStringConverter implements Converter<String, LocalDateTime> {
        @Override
        public LocalDateTime convert(String source) {
            return source == null ? null : LocalDateTime.parse(source);
        }
    }
}

0
投票

@Maksym的方式对我也很有帮助。我不得不为Spring Framework 5和Spring Boot 2稍微调整一下:

@Bean
@Primary
public MappingMongoConverter mongoConverter(
        @Autowired MongoMappingContext mongoMappingContext,
        @Autowired MongoDbFactory mainMongoFactory,
        @Autowired MongoCustomConversions conversions
) {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mainMongoFactory);
    MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
    mongoConverter.setMapKeyDotReplacement("#");
    mongoConverter.afterPropertiesSet();
    mongoConverter.setCustomConversions(conversions);
    return mongoConverter;
}


@Bean
public MongoMappingContext mongoMappingContext() {
    MongoMappingContext context = new MongoMappingContext();
    context.setSimpleTypeHolder(new SimpleTypeHolder(new HashSet<>(Arrays.asList(
            DateTime.class,
            LocalDateTime.class
    )), MongoSimpleTypes.HOLDER));
    return context;
}

@Bean
public MongoCustomConversions customConversions() {
    List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
    converterList.add(new MongoLocalDateTimeFromStringConverter());
    converterList.add(new MongoDateTimeFromStringConverter());
    return new MongoCustomConversions(converterList);
}

private static final class MongoLocalDateTimeFromStringConverter implements Converter<String, LocalDateTime> {
    @Override
    public LocalDateTime convert(String source) {
        return source == null ? null : LocalDateTime.parse(source);
    }
}

private static final class MongoDateTimeFromStringConverter implements Converter<String, DateTime> {
    @Override
    public DateTime convert(String source) {
        return source == null ? null : DateTime.parse(source);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.