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

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


慕工程0101907
浏览 220回答 2
2回答

天涯尽头无女友

该错误表明它无法构造 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;

守着星空守着你

我对 swagger 生成的 bean 也有同样的问题。为了解决这个问题,我为日期类型创建了一些序列化器和反序列化器:org. Threeten.bp.LocalDate 和 org. Threeten.bp.OffsetDateTime。而且效果很好:)。@Bean@Primarypublic ObjectMapper serializingObjectMapper() {&nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; JavaTimeModule javaTimeModule = new JavaTimeModule();&nbsp; &nbsp; javaTimeModule.addSerializer(OffsetDateTime.class, new OffsetDateTimeSerializer());&nbsp; &nbsp; javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());&nbsp; &nbsp; javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());&nbsp; &nbsp; javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());&nbsp; &nbsp; objectMapper.registerModule(javaTimeModule);&nbsp; &nbsp; return objectMapper;}public static class OffsetDateTimeSerializer extends JsonSerializer<OffsetDateTime> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(OffsetDateTime arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; arg1.writeString(arg0.toString());&nbsp; &nbsp; }}public static class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public OffsetDateTime deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; return OffsetDateTime.parse(arg0.getText());&nbsp; &nbsp; }}public static class LocalDateSerializer extends JsonSerializer<LocalDate> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(LocalDate arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; arg1.writeString(arg0.toString());&nbsp; &nbsp; }}public static class LocalDateDeserializer extends JsonDeserializer<LocalDate> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public LocalDate deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; return LocalDate.parse(arg0.getText());&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java