gpt4 book ai didi

java - Spring Boot 无法反序列化包含 OffsetDateTime 的对象

转载 作者:行者123 更新时间:2023-12-02 01:17:53 26 4
gpt4 key购买 nike

我正在尝试调用一个休息端点,它返回一个 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 :(有什么好的解决方案可以解决这个问题?

最佳答案

错误表明它无法构造 org. Threeten.bp.OffsetDateTime 的实例。您需要使用

java.time.offsetdatetime

然后在您的模型中,您可以按照您喜欢的方式对其进行格式化,例如

@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;

关于java - Spring Boot 无法反序列化包含 OffsetDateTime 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com