在Spring Data MongoDB for ZonedDateTime中注册一个可审核的新日期转换器

2022-05-13 00:00:00 java spring-data spring-data-mongodb

我希望我的可审核(@CreatedDate@LastModifiedDate)MongoDB文档使用ZonedDateTime字段。

显然,Spring Data不支持此类型(请查看org.springframework.data.auditing.AnnotationAuditingMetadata)。

框架版本:Spring Boot 2.0.0和Spring Data MongoDB 2.0.0

Spring数据审核错误:

java.lang.IllegalArgumentException: Invalid date type for member <MEMBER NAME>!
Supported types are [org.joda.time.DateTime, org.joda.time.LocalDateTime, java.util.Date, java.lang.Long, long].

Mongo配置:

@Configuration
@EnableMongoAuditing
public class MongoConfiguration {

}

可审计实体:

public abstract class BaseDocument {

    @CreatedDate
    private ZonedDateTime createdDate;

    @LastModifiedDate
    private ZonedDateTime lastModifiedDate;

}

我尝试的内容

我还尝试为ZonedDateTime创建自定义转换器,但Spring data不考虑它。类DateConvertingAuditableBeanWrapper有一个ConversionService,它在构造函数方法中配置了JodaTimeConvertersJsr310ConvertersThreeTenBackPortConverters

自定义转换器:

@Component
public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime, ZonedDateTime> {

    @Override
    public ZonedDateTime convert(LocalDateTime source) {
        return source.atZone(ZoneId.systemDefault());
    }

}

Spring Data DateConvertingAuditableBeanWrapper:

class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {

    abstract static class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper {

        private final ConversionService conversionService;

    }
}

是否可以审核ZonedDateTime个字段?

如何注册转换器?


解决方案

创建DateTimeProvider以提供审核时使用的当前时间:

@Component("dateTimeProvider")
public class CustomDateTimeProvider implements DateTimeProvider {
    
    @Override
    public Optional<TemporalAccessor> getNow() {
        return Optional.of(ZonedDateTime.now());
    }
}

然后:

  • 引用@EnableMongoAuditing批注中的DateTimeProvider组件;
  • DateZonedDateTime创建Converter
  • Converter实例添加到MongoCustomConversions实例;
  • MongoCustomConversions实例公开为@Bean
@Configuration
@EnableMongoAuditing(dateTimeProviderRef = "dateTimeProvider")
public class MongoConfiguration {
    
    @Bean
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new DateToZonedDateTimeConverter());
        converters.add(new ZonedDateTimeToDateConverter());
        return new MongoCustomConversions(converters);
    }

    class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        @Override
        public ZonedDateTime convert(Date source) {
            return source == null ? null : 
                    ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        }
    }

    class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        @Override
        public Date convert(ZonedDateTime source) {
            return source == null ? null : Date.from(source.toInstant());
        }
    }
}

但是,我不会将ZonedDateTime用于此目的。我会坚持OffsetDateTime

OffsetDateTimeZonedDateTimeInstant都以纳秒精度存储时间线上的瞬间。Instant是最简单的,简单地表示瞬间。OffsetDateTime将UTC/格林威治的偏移量添加到该时刻,这允许获取本地日期-时间。ZonedDateTime添加完整时区规则。

计划使用ZonedDateTimeInstant对更简单的应用程序中的数据进行建模。在对日期-时间概念进行更详细的建模时,或者在与数据库或网络协议进行通信时,可以使用此类。

相关文章