objectify.LoadException:java.time.LocalDateTime必须有一个no-arg构造函数

时间:2017-07-24 07:38:04

标签: java google-app-engine java-8 google-cloud-datastore objectify

我正在使用Java 8 LocalDateTime类在数据存储区中保留日期。 日期是这种格式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]

我该如何解决这个问题?

我研究过&amp;加入了我的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;

但仍然没有锻炼。任何猜测?

1 个答案:

答案 0 :(得分:2)

当前版本的Objectify没有对J8数据类型的内置支持。但是,您可以非常轻松地在应用中添加支持。

在Objectify源中查找包com.googlecode.objectify.impl.translate.opt.joda。它包含允许Objectify使用joda时间对象的翻译器;在注册实体类之前,请使用ObjectifyFactory注册这些内容。应该明白它们是如何工作的;您只需要能够在LocalDateTime之间进行转换,但是您希望它存储在数据存储区中。

一点警告:不要试图将LocalDateTime转换为java.util.Date,这代表了一个瞬间。您的LocalDateTime没有TZ,因此不是即时的。最好将其表示为ISO 8601字符串。看看ReadablePartialTranslatorFactory ......尽管你的用例可能不需要反思。

相关问题