[英]Mongo auditable ZonedDateTime fields don't work in Spring Boot 2
[英]Register a new Date Converter Auditable in 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
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;
}
我也尝试为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());
}
}
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
都会在时间线上存储一个纳秒精度的瞬间。 即时是最简单的,只是代表瞬间。OffsetDateTime
将UTC / Greenwich的偏移量添加到瞬间,从而可以获得本地日期时间。ZonedDateTime
添加了完整的时区规则。
ZonedDateTime
或Instant
用于在更简单的应用程序中建模数据。 在更详细地建模日期时间概念时,或者在与数据库或网络协议进行通信时,可以使用此类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.