我想要我的可审计(@CreatedDate
and @LastModifiedDate
) 要使用的 MongoDB 文档ZonedDateTime
fields.
显然 Spring Data 不支持这种类型(看看org.springframework.data.auditing.AnnotationAuditingMetadata
).
框架版本:春季启动2.0.0 and Spring数据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].
MongoDB配置:
@Configuration
@EnableMongoAuditing
public class MongoConfiguration {
}
可审计实体:
public abstract class BaseDocument {
@CreatedDate
private ZonedDateTime createdDate;
@LastModifiedDate
private ZonedDateTime lastModifiedDate;
}
我尝试过的事情
我还尝试创建一个自定义转换器ZonedDateTime
,但 Spring Data 不考虑它。班上DateConvertingAuditableBeanWrapper
has a ConversionService
在构造函数方法中配置为JodaTimeConverters
, Jsr310Converters
and 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
fields?
如何注册转换器?