繁体   English   中英

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

[英]Register a new Date Converter Auditable in Spring Data MongoDB for ZonedDateTime

我希望我的可审计( @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].

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

接着:

@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都会在时间线上存储一个纳秒精度的瞬间。 即时是最简单的,只是代表瞬间。 OffsetDateTime将UTC / Greenwich的偏移量添加到瞬间,从而可以获得本地日期时间。 ZonedDateTime添加了完整的时区规则。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM