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中注册一个可审核的新日期转换器
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 转换 ldap 日期 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
