cir*_*rvo 12 java spring-data spring-data-mongodb
我希望我的可审计(@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].
Run Code Online (Sandbox Code Playgroud)
@Configuration
@EnableMongoAuditing
public class MongoConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
public abstract class BaseDocument {
@CreatedDate
private ZonedDateTime createdDate;
@LastModifiedDate
private ZonedDateTime lastModifiedDate;
}
Run Code Online (Sandbox Code Playgroud)
我也试过为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());
}
}
Run Code Online (Sandbox Code Playgroud)
class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {
abstract static class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper {
private final ConversionService conversionService;
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以审核ZonedDateTime字段?
如何注册转换器?
创建一个DateTimeProvider以提供审核时要使用的当前时间:
@Component("dateTimeProvider")
public class CustomDateTimeProvider implements DateTimeProvider {
@Override
public Optional<TemporalAccessor> getNow() {
return Optional.of(ZonedDateTime.now());
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
DateTimeProvider组件@EnableMongoAuditing;Converter为Date和创建ZonedDateTime;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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不会ZonedDateTime为此目的使用。我会坚持OffsetDateTime:
OffsetDateTime,ZonedDateTime并且Instant它们在时间轴上存储的时间点都达到了纳秒级的精度。即时是最简单的,仅表示即时。OffsetDateTime将与UTC /格林威治时间的偏移量添加到瞬间,从而可以获取本地日期时间。ZonedDateTime添加完整的时区规则。旨在
ZonedDateTime或Instant用于在较简单的应用程序中对数据建模。在更详细地建模日期时间概念时,或者在与数据库或网络协议进行通信时,可以使用此类。
| 归档时间: |
|
| 查看次数: |
2717 次 |
| 最近记录: |