Java 8 Date/Time (JSR-310) types mapping with Spring Data MongoDB

后端 未结 2 688
醉话见心
醉话见心 2020-12-14 22:17

I have simple document with Java 8 date/time fields

@Document
public class Token {
    private Instant createdAt;
    ...
}

that I want to

相关标签:
2条回答
  • 2020-12-14 22:58

    I don't know if this is the best way but I added Java 8 Date/Time (JSR-310) types support to Spring Data MongoDB 1.5.0.RELEASE like this:

    1. First step. Add simple Spring Converters

      public class InstantToLongConverter implements Converter<Instant, Long> {
          @Override
          public Long convert(Instant instant) {
              return instant.toEpochMilli();
          }
      }
      
      public class LongToInstantConverter implements Converter<Long, Instant> {
          @Override
          public Instant convert(Long source) {
              return Instant.ofEpochMilli(source);
          }
      }
      
      public class LocalDateToStringConverter implements Converter<LocalDate, String> {
          @Override
          public String convert(LocalDate localDate) {
              return localDate.toString();
          }
      }
      
      public class StringToLocalDateConverter implements Converter<String, LocalDate> {
          @Override
          public LocalDate convert(String source) {
              return LocalDate.parse(source);
          }
      }
      
    2. Second step. Register these custom Converters with MappingMongoConverter in your AbstractMongoConfiguration implementation like this:

      @Configuration
      @EnableMongoRepositories(basePackages = {"my.app.repository"})
      public class MongoConfiguration extends AbstractMongoConfiguration {
      
          ...
      
          @Override
          public CustomConversions customConversions() {
              return new CustomConversions(Arrays.asList(
                      new InstantToLongConverter(), new LongToInstantConverter(),
                      new LocalDateToStringConverter(), new StringToLocalDateConverter()));
          }
      }
      

    Now your document's Instant fields will be persisted as long values and LocalDates as Strings.

    0 讨论(0)
  • 2020-12-14 23:01

    @user882209 explained it all just perfectly.
    Since Spring Data MongoDB 1.7 the support for JSR-310 has been added.
    If application is backed by Spring Boot every version over 1.2.8 would contain it as well.
    In a Spring Boot-ed app you can just do it the following:

    @Configuration
    public class MongoDbConfig {
    
        @Autowired
        private MongoDbFactory mongoDbFactory;
    
        @Bean
        public MongoTemplate mongoTemplate() {
            MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
                new MongoMappingContext());
            converter.setCustomConversions(new CustomConversions(Arrays.asList(...)));
    
        return new MongoTemplate(mongoDbFactory, converter);
        }
    }
    

    The following converters are provided by the Jsr310Converters class:
    DateToLocalDateTimeConverter - LocalDateTimeToDateConverter DateToLocalDateConverter - LocalDateToDateConverter DateToLocalTimeConverter - LocalTimeToDateConverter DateToInstantConverter - InstantToDateConverter

    0 讨论(0)
提交回复
热议问题