在Spring Data MongoDB for ZonedDateTime中注册一个可审计的新Date Converter

cir*_*rvo 12 java spring-data spring-data-mongodb

我希望我的可审计(@CreatedDate@LastModifiedDate)MongoDB文档可以使用ZonedDateTime字段.

显然,Spring Data不支持这种类型(请看一下org.springframework.data.auditing.AnnotationAuditingMetadata).

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

Spring Data审核错误:

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].
Run Code Online (Sandbox Code Playgroud)

Mongo配置:

@Configuration
@EnableMongoAuditing
public class MongoConfiguration {

}
Run Code Online (Sandbox Code Playgroud)

可审计实体:

public abstract class BaseDocument {

    @CreatedDate
    private ZonedDateTime createdDate;

    @LastModifiedDate
    private ZonedDateTime lastModifiedDate;

}
Run Code Online (Sandbox Code Playgroud)

我试过的事情

我也试过为ZonedDateTime它创建一个自定义转换器,但Spring Data没有考虑它.该类DateConvertingAuditableBeanWrapper有一个ConversionService在构造函数方法中配置的JodaTimeConverters,Jsr310ConvertersThreeTenBackPortConverters.

定制转换器:

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

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

}
Run Code Online (Sandbox Code Playgroud)

Spring Data DateConvertingAuditableBeanWrapper:

class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {

    abstract static class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper {

        private final ConversionService conversionService;

    }
}
Run Code Online (Sandbox Code Playgroud)

是否可以审核ZonedDateTime字段?

如何注册转换器?

cas*_*lin 8

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

@Component("dateTimeProvider")
public class CustomDateTimeProvider implements DateTimeProvider {

    @Override
    public Optional<TemporalAccessor> getNow() {
        return Optional.of(ZonedDateTime.now());
    }
}
Run Code Online (Sandbox Code Playgroud)

然后:

@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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我不会ZonedDateTime为此目的使用。我会坚持OffsetDateTime

OffsetDateTimeZonedDateTime并且Instant它们在时间轴上存储的时间点都达到了纳秒级的精度。即时是最简单的,仅表示即时。OffsetDateTime将与UTC /格林威治时间的偏移量添加到瞬间,从而可以获取本地日期时间。ZonedDateTime添加完整的时区规则。

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