[英]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.0和Spring Data MongoDB 2.0.0
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].
@Configuration
@EnableMongoAuditing
public class MongoConfiguration {
}
public abstract class BaseDocument {
@CreatedDate
private ZonedDateTime createdDate;
@LastModifiedDate
private ZonedDateTime lastModifiedDate;
}
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
其在与所述构造方法构造JodaTimeConverters
, Jsr310Converters
和ThreeTenBackPortConverters
。
@Component
public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime, ZonedDateTime> {
@Override
public ZonedDateTime convert(LocalDateTime source) {
return source.atZone(ZoneId.systemDefault());
}
}
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: 接着:
DateTimeProvider
component in the @EnableMongoAuditing
annotation; @EnableMongoAuditing
注释中引用DateTimeProvider
组件; Converter
s for Date
and ZonedDateTime
; Date
和ZonedDateTime
创建Converter
; Converter
instances to a MongoCustomConversions
instance; Converter
实例添加到MongoCustomConversions
实例; MongoCustomConversions
instance as a @Bean
. 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());
}
}
}
I wouldn't, however, use ZonedDateTime
for this purpose. 但是,我不会为此目的使用
ZonedDateTime
。 I would stick to OffsetDateTime
: 我会坚持
OffsetDateTime
:
OffsetDateTime
,ZonedDateTime
andInstant
all store an instant on the time-line to nanosecond precision.OffsetDateTime
,ZonedDateTime
和Instant
都会在时间线上存储一个纳秒精度的瞬间。 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
orInstant
is used to model data in simpler applications.ZonedDateTime
或Instant
用于在更简单的应用程序中建模数据。 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.