【问题标题】:objectify.LoadException: java.time.LocalDateTime must have a no-arg constructorobjectify.LoadException:java.time.LocalDateTime 必须有一个无参数构造函数
【发布时间】:2017-07-24 07:38:03
【问题描述】:

我正在使用 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]

我该如何解决这个问题?

我研究并添加到我的 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;

但它仍然没有锻炼。有什么猜测吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    一点警告:不要试图将LocalDateTime 转换为java.util.Date,它代表一个瞬间。您的 LocalDateTime 没有 TZ,因此不是瞬间。最好将其表示为 ISO 8601 字符串。看看ReadablePartialTranslatorFactory...虽然反射对于您的用例来说可能是不必要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      相关资源
      最近更新 更多