微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法从字符串反序列化LocalDateTime类型的值

如何解决无法从字符串反序列化LocalDateTime类型的值

我有以下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解码的问题,特别是从LocalDateTimeDuration的转换时。有想法吗?

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

谢谢。 ;)

解决方法

LocalDateTime无法转换为Duration,反之亦然。除了Serializable(当然还有Object)之外,在他们的层次结构中没有其他共同之处。

替换

private LocalDateTime registrationDate;

使用

private Duration registrationDate;

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

,

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

如果您从返回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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。