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中注册一个可审核的新日期转换器
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
