我正在使用 Java 8LocalDateTime
类在数据存储中保留日期。日期是这种格式2017-07-24T01:00:00.000
作为 EmbeddedEntity 持久化。
private LocalDateTime matchDateTime;
持久化流没问题。但是当我加载实体时抛出异常。
com.googlecode.objectify.LoadException: Error loading : java.time.LocalDateTime must have a no-arg constructor
at com.googlecode.objectify.impl.EntityMetadata.load(EntityMetadata.java:78) ~[objectify-5.1.21.jar:na]
at com.googlecode.objectify.impl.LoadEngine.load(LoadEngine.java:185) ~[objectify-5.1.21.jar:na]
Caused by: java.lang.IllegalStateException: java.time.LocalDateTime must have a no-arg constructor
at com.googlecode.objectify.impl.TypeUtils.getNoArgConstructor(TypeUtils.java:47) ~[objectify-5.1.21.jar:na]
at com.googlecode.objectify.ObjectifyFactory.construct(ObjectifyFactory.java:69) ~[objectify-5.1.21.jar:na]
Caused by: java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_131]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_131]
我该如何解决?
我研究并添加到我的 pom.xml 文件中。
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.9</version>
</dependency>
将日期属性标记为
@JsonSerialize(using = ToStringSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime matchDateTime;
但它仍然没有锻炼。有什么猜测吗?
当前版本的 Objectify 没有对 J8 数据类型的内置支持。但是,您可以非常轻松地在您的应用程序中添加支持。
查看包的 Objectify 源代码com.googlecode.objectify.impl.translate.opt.joda
。它包含允许 Objectify 使用 joda 时间对象的转换器;ObjectifyFactory
在注册实体类之前,您可以使用 注册这些。它们的工作方式应该很明显;您只需要能够在LocalDateTime
您希望将其存储在数据存储中的方式和方式之间进行转换。
一点警告:不要试图转换LocalDateTime
为java.util.Date
,它代表一个瞬间。你LocalDateTime
没有 TZ,因此不是瞬间。最好将其表示为 ISO 8601 字符串。看看ReadablePartialTranslatorFactory
...虽然反射对于您的用例来说可能是不必要的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句