Tru*_*rin 4 java spring object mapper spring-boot
我正在尝试调用一个休息端点,它返回一个 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
}
Run Code Online (Sandbox Code Playgroud)
这是调用:
ResponseEntity<Process> responseModel = restTemplate.exchange("http://localhost:8062/processes", HttpMethod.POST, httpEntity, Process.class);
Run Code Online (Sandbox Code Playgroud)
问题是我尝试了一些方法,例如忽略 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')
Run Code Online (Sandbox Code Playgroud)
或者它将返回 null :( 解决这个问题的好解决方案是什么?
该错误表明它无法构造 org. Threeten.bp.OffsetDateTime 的实例。你需要使用
java.time.offsetdatetime
Run Code Online (Sandbox Code Playgroud)
然后在你的模型中你可以按照你喜欢的方式格式化它,例如
@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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5592 次 |
| 最近记录: |