【问题标题】:Could not read JSON: Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist)无法读取 JSON:无法构造 `java.time.ZonedDateTime` 的实例(没有创建者,如默认构造,存在)
【发布时间】:2020-11-23 15:34:50
【问题描述】:

我有一个侦听队列然后将其映射到 POJO 的服务。 但即使在设置了 ObjectMapper 的@Configuration 之后,我总是会收到此错误

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.registerModule(new JavaTimeModule());

        return mapper;
    }

我的 POJO:

public class ResultDto {
    private ZonedDateTime dateSent;

    private ZonedDateTime dateDeliveryReceiptReceived;

    public ResultDto() {}
}

我收到此错误:

Caused by: org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot construct instance of `java.time.ZonedDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-08-03T11:02:51.044+0000')

提前致谢!

【问题讨论】:

    标签: java spring-boot zoneddatetime


    【解决方案1】:

    使用 @JsonFormat(pattern = 'specify pattern here')

    ObjectMapper 默认尝试在构造函数中使用 String 创建 ZonedDateTime 对象,但不存在这样的东西。通过添加此注释,您将允许它使用给定格式从 String 解析它。

    【讨论】:

    • 我仍然遇到同样的错误 :( 有我试过的模式:@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX") @ JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    • 试试“yyyy-MM-dd'T'HH:mm:ss.SSSXXXX”
    • 您好,先生。仍然没有。 :(
    • 模块好像没有注册
    【解决方案2】:

    从错误来看,它似乎正在寻找String-argument 构造函数。在ResultDto中添加以下构造函数后尝试:

    public ResultDto(ZonedDateTime dateSent, ZonedDateTime dateDeliveryReceiptReceived) {
        this.dateSent = dateSent;
        this.dateDeliveryReceiptReceived = dateDeliveryReceiptReceived;
    }
    
    public ResultDto(String dateSent, String dateDeliveryReceiptReceived) {
        this.dateSent = ZonedDateTime.parse(dateSent, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
        this.dateDeliveryReceiptReceived = ZonedDateTime.parse(dateDeliveryReceiptReceived,
                DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
    }
    

    【讨论】:

      【解决方案3】:

      感谢那些回答的人。

      在队友的帮助下,我们发现spring cloud有自己的object mapper。而不是直接ObjectMapper。由于此 DTO/POJO 是来自 AWS SNS/SQS 的消息。

      应该这样做:

      @Bean
      public MappingJackson2MessageConverter mappingJackson2MessageConverter(ObjectMapper objectMapper) {
          MappingJackson2MessageConverter jacksonMessageConverter = new MappingJackson2MessageConverter();
          jacksonMessageConverter.setObjectMapper(objectMapper);
          jacksonMessageConverter.setSerializedPayloadClass(String.class);
          jacksonMessageConverter.setStrictContentTypeMatch(true);
          return jacksonMessageConverter;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-11-05
        • 2020-01-01
        • 2020-12-15
        • 2020-04-17
        • 1970-01-01
        • 2022-01-05
        • 2017-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多