简体   繁体   English

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

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

I want my auditable ( @CreatedDate and @LastModifiedDate ) MongoDB document to work with ZonedDateTime fields. 我希望我的可审计( @CreatedDate@LastModifiedDate )MongoDB文档与ZonedDateTime字段一起使用。

Apparently this type is not supported by Spring Data (have a look at org.springframework.data.auditing.AnnotationAuditingMetadata ). 显然,Spring Data不支持这种类型(请查看org.springframework.data.auditing.AnnotationAuditingMetadata )。

Framework version: Spring Boot 2.0.0 and Spring Data MongoDB 2.0.0 框架版本: Spring Boot 2.0.0Spring Data MongoDB 2.0.0

Spring Data auditing error: 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: Mongo配置:

@Configuration
@EnableMongoAuditing
public class MongoConfiguration {

}

The auditable entity: 可审计实体:

public abstract class BaseDocument {

    @CreatedDate
    private ZonedDateTime createdDate;

    @LastModifiedDate
    private ZonedDateTime lastModifiedDate;

}

Things I tried 我试过的事情

I also tried creating a custom converter for ZonedDateTime , but it is not considered by Spring Data. 我也尝试为ZonedDateTime创建自定义转换器,但Spring Data不考虑它。 The class DateConvertingAuditableBeanWrapper has a ConversionService which is configured in the constructor method with JodaTimeConverters , Jsr310Converters and ThreeTenBackPortConverters . DateConvertingAuditableBeanWrapper具有ConversionService其在与所述构造方法构造JodaTimeConvertersJsr310ConvertersThreeTenBackPortConverters

Custom converter: 定制转换器:

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

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

}

Spring Data DateConvertingAuditableBeanWrapper: Spring Data DateConvertingAuditableBeanWrapper:

class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {

    abstract static class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper {

        private final ConversionService conversionService;

    }
}

Is it possible to audit ZonedDateTime fields? 是否可以审核ZonedDateTime字段?

How can I register a converter? 如何注册转换器?

Create a DateTimeProvider to provide the current time to be used when auditing: 创建DateTimeProvider以提供审计时使用的当前时间:

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

    @Override
    public Optional<TemporalAccessor> getNow() {
        return Optional.of(ZonedDateTime.now());
    }
}

And then: 接着:

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

I wouldn't, however, use ZonedDateTime for this purpose. 但是,我不会为此目的使用ZonedDateTime I would stick to OffsetDateTime : 我会坚持OffsetDateTime

OffsetDateTime , ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. OffsetDateTimeZonedDateTimeInstant都会在时间线上存储一个纳秒精度的瞬间。 Instant is the simplest, simply representing the instant. 即时是最简单的,只是代表瞬间。 OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. OffsetDateTime将UTC / Greenwich的偏移量添加到瞬间,从而可以获得本地日期时间。 ZonedDateTime adds full time-zone rules. ZonedDateTime添加了完整的时区规则。

It is intended that ZonedDateTime or Instant is used to model data in simpler applications. ZonedDateTimeInstant用于在更简单的应用程序中建模数据。 This class may be used when modeling date-time concepts in more detail, or when communicating to a database or in a network protocol. 在更详细地建模日期时间概念时,或者在与数据库或网络协议进行通信时,可以使用此类。

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

相关问题 Mongo 可审计的 ZonedDateTime 字段在 Spring Boot 2 中不起作用 - Mongo auditable ZonedDateTime fields don't work in Spring Boot 2 spring data mongo 不能 pikup 自定义 ZonedDateTime 转换器,为什么? - spring data mongo can't pikup custom ZonedDateTime converter, why? Spring Boot 按日期搜索数据(以 ZonedDateTime 格式) - Spring Boot Search Data by Date (formated in ZonedDateTime) Spring REST API 用于pojo的注册日期转换器 - Spring REST API register date converter for pojo 用于枚举字段的mongoDb和spring数据自定义转换器 - mongoDb and spring data custom converter for enum field 春季启动2:ConverterNotFoundException:未找到能够从类型[java.time.ZonedDateTime]转换为类型[java.util.Date]的转换器 - Spring boot 2 : ConverterNotFoundException: No converter found capable of converting from type [java.time.ZonedDateTime] to type [java.util.Date] 如何使用 Spring Data MongoDB 插入新日期? - How can I do a insert a new Date with Spring Data MongoDB? 我是否需要为Spring Data中的“可审核”字段提供获取器/设置器? - Do I need to provide getters/setters for Auditable fields in Spring Data? 为ZonedDateTime编写xstream转换器 - Writing xstream Converter for ZonedDateTime Spring 数据 MongoDB 字段转换器:有什么方法可以传递参数? - Spring Data MongoDB Field Converter: Any way to pass a parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM