Register a new Date Converter Auditable in Spring Data MongoDB for ZonedDateTime(在Spring Data MongoDB for ZonedDateTime中注册一个可审核的新日期转换器)
问题描述
我希望我的可审核(@CreatedDate和@LastModifiedDate)MongoDB文档使用ZonedDateTime字段。
显然,Spring Data不支持此类型(请查看org.springframework.data.auditing.AnnotationAuditingMetadata)。
框架版本:Spring Boot 2.0.0和Spring 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,它在构造函数方法中配置了JodaTimeConverters、Jsr310Converters和ThreeTenBackPortConverters。
自定义转换器:
@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());
}
}
然后:
- 引用
@EnableMongoAuditing批注中的DateTimeProvider组件; - 为
Date和ZonedDateTime创建Converter; - 将
Converter实例添加到MongoCustomConversions实例; - 将
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());
}
}
}
但是,我不会将
ZonedDateTime用于此目的。我会坚持OffsetDateTime:
OffsetDateTime、ZonedDateTime和Instant都以纳秒精度存储时间线上的瞬间。Instant是最简单的,简单地表示瞬间。OffsetDateTime将UTC/格林威治的偏移量添加到该时刻,这允许获取本地日期-时间。ZonedDateTime添加完整时区规则。 计划使用ZonedDateTime或Instant对更简单的应用程序中的数据进行建模。在对日期-时间概念进行更详细的建模时,或者在与数据库或网络协议进行通信时,可以使用此类。
这篇关于在Spring Data MongoDB for ZonedDateTime中注册一个可审核的新日期转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Spring Data MongoDB for ZonedDateTime中注册一个可审核的新日期转换器
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 从 python 访问 JVM 2022-01-01
