【问题标题】:Cannot deserialize value of type LocalDateTime from String无法从 String 反序列化 LocalDateTime 类型的值
【发布时间】:2020-10-23 17:36:35
【问题描述】:

我有以下DTOEntity

public class PaymentDto {

    private String provider;

    private Duration timeDifferenceDate;

    public PaymentDto() {
        // Empty for framework
    }

    public PaymentDto(Payment payment) {
        this.provider = payment.getProvider();
        this.setRegistrationDate(payment.getRegistrationDate());
    }

    public Duration getRegistrationDate() {
        return timeDifferenceDate;
    }

    public void setRegistrationDate(LocalDateTime registrationDate) {
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(now, registrationDate);
        this.timeDifferenceDate = duration;
    }

}
public class Payment {

    private LocalDateTime registrationDate;

    public Payment() {
        // Empty for framework
    }

但是当它从Payment 转换为PaymentDto 时,我遇到了JSON 解码问题,特别是从LocalDateTime 转换为Duration。有什么想法吗?

    @Override
    public List<PaymentDto> readAll() {
        return this.paymentPersistence.readAll().stream()
                .map(PaymentDto::new).collect(Collectors.toList());
    }
org.springframework.core.codec.DecodingException: JSON decoding error: Cannot deserialize value of type `java.time.LocalDateTime` from String "PT-1.015005S": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text 'PT-1.015005S' could not be parsed at index 0; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "PT-1.015005S": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text 'PT-1.015005S' could not be parsed at index 0
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.user.rest.dtos.PaymentDto["registrationDate"])

    at org.springframework.http.codec.json.AbstractJackson2Decoder.processException(AbstractJackson2Decoder.java:215)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ Body from GET http://localhost:61072/payments [DefaultClientResponse]
Stack trace:
        at org.springframework.http.codec.json.AbstractJackson2Decoder.processException(AbstractJackson2Decoder.java:215)

顺便谢谢你。 ;)

【问题讨论】:

    标签: java json serialization duration localdatetime


    【解决方案1】:

    LocalDateTime 不能转换为Duration,反之亦然。除了Serializable(当然还有Object),它们的层次结构中没有什么共同点。

    替换

    private LocalDateTime registrationDate;
    

    private Duration registrationDate;
    

    或者创建一个类型为Duration的新实例变量。

    【讨论】:

      【解决方案2】:

      作为@Arvind Kumar Avinash mentioned above,您需要在设置器PaymentDto::setRegistrationDate 中提供适当的类型Duration

      如果您从返回LocalDateTime 字段的实体填充DTO,您还应该修改“转换”构造函数。此外,在计算持续时间时,您应该将registrationDate 放在首位,以避免“负”持续时间(较早的时刻优先)。

      public PaymentDto(Payment payment) {
          this.provider = payment.getProvider();
          this.setRegistrationDate(Duration.between(
              payment.getRegistrationDate(),  // older "start" date should go first
              LocalDateTime.now()
          ));
      }
      
      public void setRegistrationDate(Duration timeDifference) 
          this.timeDifferenceDate = timeDifference;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-09-06
        • 2017-10-19
        • 2019-01-03
        • 2023-01-25
        • 2023-02-02
        • 1970-01-01
        • 2019-02-16
        • 2019-08-09
        • 1970-01-01
        相关资源
        最近更新 更多