在Spring Data MongoDB for ZonedDateTime中注册一个可审核的新日期转换器 [英] Register a new Date Converter Auditable in Spring Data MongoDB for ZonedDateTime

查看:58
本文介绍了在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数据审核错误:

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

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

这篇关于在Spring Data MongoDB for ZonedDateTime中注册一个可审核的新日期转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆