objectify.LoadException: java.time.LocalDateTime 必须有一个无参数的构造函数
objectify.LoadException: java.time.LocalDateTime must have a no-arg constructor
我正在使用 Java 8 LocalDateTime
class 在数据存储中保存日期。
日期是这种格式 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 中。
<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
... 虽然反射对于您的用例可能是不必要的。
我正在使用 Java 8 LocalDateTime
class 在数据存储中保存日期。
日期是这种格式 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 中。
<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
... 虽然反射对于您的用例可能是不必要的。