保存包含新Java 8 java.time.LocalDateTime
的对象时,将引发以下错误:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date]
经过测试
- 春天4.3.2发布
- Spring Data MongoDB 1.9.2.RELEASE
Spring-data是否支持新的Java 8 Date API(JSR-310)?
1. Spring Data + JSR-310
是的,Spring-data通过定制转换器– Jsr310Converters
支持JSR-310规范,请查看以下代码片段:
CustomConversions.java
package org.springframework.data.mongodb.core.convert;
public class CustomConversions {
/**
* Creates a new {@link CustomConversions} instance registering the given converters.
*
* @param converters
*/
public CustomConversions(List<?> converters) {
//...
List<Object> toRegister = new ArrayList<Object>();
// Add user provided converters to make sure they can override the defaults
toRegister.addAll(converters);
toRegister.add(CustomToStringConverter.INSTANCE);
toRegister.addAll(MongoConverters.getConvertersToRegister());
toRegister.addAll(JodaTimeConverters.getConvertersToRegister());
toRegister.addAll(GeoConverters.getConvertersToRegister());
toRegister.addAll(Jsr310Converters.getConvertersToRegister());
toRegister.addAll(ThreeTenBackPortConverters.getConvertersToRegister());
for (Object c : toRegister) {
registerConversion(c);
}
//...
}
2.解决方案
如果您为MappingMongoConverter
声明一个Spring托管bean并作为MongoTemplate
的参数MongoTemplate
,请查看以下MongoConfig示例,以启用上述Jsr310Converters
:
MongoConfig.java
//...
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
@Configuration
@ComponentScan(basePackages = {"com.mkyong.db"})
public class MongoConfig {
//...
@Autowired
MongoDbFactory mongoDbFactory;
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, getDefaultMongoConverter());
return mongoTemplate;
}
@Bean
public MappingMongoConverter getDefaultMongoConverter() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(
new DefaultDbRefResolver(mongoDbFactory), new MongoMappingContext());
return converter;
}
//...
}
MappingMongoConverter
扩展了AbstractMongoConverter
,而AbstractMongoConverter
是InitializingBean
。 如果通过@Bean
将其声明为Spring托管的bean,则将afterPropertiesSet()
并注册所有默认转换器,包括Jsr310Converters
3.常见问题
如果MappingMongoConverter
不是由Spring管理的(使用如下所示的new
关键字进行初始化),则必须手动调用.afterPropertiesSet()
。
MongoConfig.java
@Configuration
@ComponentScan(basePackages = {"com.mkyong.db"})
public class MongoConfig {
@Autowired
MongoDbFactory mongoDbFactory;
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(
new DefaultDbRefResolver(mongoDbFactory), new MongoMappingContext());
//CALL THIS MANULLY, so that all the default convertors will be registered!
converter.afterPropertiesSet();
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);
return mongoTemplate;
}
//...
参考文献
- Github –自定义转换器到spring-data-mongodb
- Spring IO – MappingMongoConverter JavaDoc
- SpringIO – CustomConversions JavaDoc
翻译自: https://mkyong.com/mongodb/spring-data-mongodb-jsr-310-or-java-8-new-date-apis/