0

我有以下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
    }

但是当它从Paymentto转换为PaymentDtoJSON 解码时,我遇到了问题,特别是从LocalDateTimeto的转换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)

顺便谢谢。;)

4

2 回答 2

2

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

代替

private LocalDateTime registrationDate;

private Duration registrationDate;

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

于 2020-10-23T17:48:18.573 回答
2

正如上面提到的@Arvind Kumar Avinash ,您需要Duration在 setter中提供适当的类型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;
}
于 2020-10-23T18:09:37.437 回答