无法从字符串反序列化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;
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...