从Java 8 LocalDateTime映射到MongoDB的异常

时间:2018-11-27 20:09:55

标签: mongodb type-conversion spring-data spring-data-mongodb localdate

我似乎在将具有java.time.LocalDateTime字段的数据库实体写入MongoDB表时遇到问题。我读过的所有内容似乎都表明这应该不是2014年末左右的问题,但由于某些原因,我仍然会遇到问题:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleWrite(MappingMongoConverter.java:849) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeSimpleInternal(MappingMongoConverter.java:829) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeProperties(MappingMongoConverter.java:488) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:462) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:436) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:391) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:86) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.toDocument(MongoTemplate.java:1070) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1253) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
...

我有一个定制的MongoConfig,看起来像这样。我尝试将其删除,但这并不能解决问题,因此,这里的问题似乎不存在,但是这里是:

@Configuration
public class MongoConfig {

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

@Autowired
private MongoDbFactory mongoDbFactory;

public @Bean
MongoDbFactory mongoDBFactory() {
    return new SimpleMongoDbFactory(new MongoClient(), database);
}

@Bean
public MongoTemplate mongoTemplate() {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);

    // Remove _class
    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, new MongoMappingContext());
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    return new MongoTemplate(mongoDBFactory(), converter);
}

}

如果需要,我可以编写一个自定义转换器为我完成此操作,但是我阅读的所有文档都说此错误已在2014年11月前后修复,因此我想知道自己做错了什么。有任何想法吗?谢谢。

0 个答案:

没有答案