首页 > 解决方案 > Jackson 无法将 String 转换为 OffsetDateTime

问题描述

我有一堂课,比如

import java.time.OffsetDateTime;

public class AState {
    AName name;
    String message;
    OffsetDateTime updatedAt;


public AssetStatusState() {
  }
}
//getters and setters follow

在我的项目中作为 Maven 依赖项导入。在我的项目中,我从事件总线接收到一个 JSON 字符串,并尝试将其解析为一个类型为 的对象AState。JSON 字符串的格式为:

{"name":"comps", "message":"ok", "updatedAt":"2020-03-05T02:31:17.683Z"}

我尝试通过以下方式解析:

this.objectMapper.convertValue(someString, AState.class)

当我构建我的主要对象时,我也会这样做

this.objectMapper.register(new JavaTimeModule());

但我仍然无法解决这个错误 - 我不知道我做错了什么,有人可以帮忙吗?

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
Cannot construct instance of `java.time.OffsetDateTime` 
(no Creators, like default construct, exist): no String-argument constructor/factory method 
to deserialize from String value ('2020-03-05T02:31:17.683Z')
 at [Source: UNKNOWN; line: -1, column: -1]

然而,在独立测试中,反序列化就像一个魅力。JSON字符串有问题吗?

标签: javajsonjackson

解决方案


推荐阅读