簡體   English   中英

無法從 String 反序列化 LocalDateTime 類型的值

[英]Cannot deserialize value of type LocalDateTime from String

我有以下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,您需要在setter 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM