Spring Boot无法反序列化具有consetis OffsetDateTime的对象

问题描述 投票:0回答:1

我试图调用一个休息端点,该端点返回一个看起来像这样的pojo对象:

public class Process   {
  @JsonProperty("id")
  private String id = null;

  @JsonProperty("processDefinitionId")
  private String processDefinitionId = null;

  @JsonProperty("businessKey")
  private String businessKey = null;

  @JsonProperty("startedAt")
  private OffsetDateTime startedAt = null;

  @JsonProperty("endedAt")
  private OffsetDateTime endedAt = null;

  @JsonProperty("durationInMs")
  private Integer durationInMs = null;

  @JsonProperty("startActivityDefinitionId")
  private String startActivityDefinitionId = null;

  @JsonProperty("endActivityDefinitionId")
  private String endActivityDefinitionId = null;

  @JsonProperty("startUserId")
  private String startUserId = null;

  @JsonProperty("deleteReason")
  private String deleteReason = null;

  //constructors and setters+getters
}

这里是电话:

ResponseEntity<Process> responseModel = restTemplate.exchange("http://localhost:8062/processes", HttpMethod.POST, httpEntity, Process.class);

问题是我尝试了一些方法,例如忽略OffsetDateTime属性或尝试更改该日期的格式,但是它将引发此错误:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.threeten.bp.OffsetDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-10-04T13:20:29.315Z')

否则它将返回null :(什么是解决此问题的好方法?

java spring spring-boot object mapper
1个回答
1
投票

错误指出它无法构造org.threeten.bp.OffsetDateTime的实例。您需要使用

java.time.offsetdatetime

然后您可以在模型中随意设置格式,例如

@JsonProperty("endedAt")@JsonProperty("endedAt") //this line is not needed when it is the same as the instance variable name
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
private OffsetDateTime endedAt;
© www.soinside.com 2019 - 2024. All rights reserved.