Spring Data MongoDB + JSR-310 or Java 8 new Date APIs 修改教程

While saving an object containing the new Java 8 java.time.LocalDateTime, the following error is thrown :

org.springframework.core.convert.ConverterNotFoundException: 
    No converter found capable of converting 
               from type [java.time.LocalDateTime] to type [java.util.Date]

Tested

  1. Spring 4.3.2.RELEASE
  2. Spring Data MongoDB 1.9.2.RELEASE

Is Spring-data supporting the new Java 8 Date APIs (JSR-310)?

1. Spring Data + JSR-310

Yes, Spring-data supports the JSR-310 spec, with a custom convertor – Jsr310Converters, review the following code snippet :

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. Solution

The above Jsr310Converters will be enabled if you declared a Spring managed bean for MappingMongoConverter, and pass as an argument for MongoTemplate, review the following MongoConfig example :

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;
    }

    //...

}

The MappingMongoConverter extends AbstractMongoConverter, and the AbstractMongoConverter is an InitializingBean. If this declared as a Spring managed bean via @Bean, the afterPropertiesSet() will be fired, and registered all the default convertors, including the Jsr310Converters

3. FAQs

If the MappingMongoConverter is not managed by Spring (init with a new keyword like below), you must call the .afterPropertiesSet() manually.

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;

    }

    //...

References

  1. Github – Custom converters to spring-data-mongodb
  2. Spring IO – MappingMongoConverter JavaDoc
  3. Spring IO – CustomConversions JavaDoc

如果觉得JSRUN的教程对您有用,请随意打赏。您的支持将鼓励JSRUN继续提供更好的服务

捐助JSRUN
JSRUN前端教程共编系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的本前端教程,让知识的积累变得统一完整、自成体系。 希望广大工程师能一起参与进共编,让零散在世界各处的知识点找到组织、找到家。
X
支付宝
9.99
请使用支付宝扫码支付, 有效时间 5分0秒
超过有效时间后请立即停止支付。
支付故障请联系QQ:565830900
因10.00元通道已占用,系统建议您支付9.99元
正在生成二维码, 此过程可能需要15秒钟
谢谢支持,我一定会更加努力的