简体   繁体   English

无法从 String 反序列化 LocalDateTime 类型的值

[英]Cannot deserialize value of type LocalDateTime from String

I have the following DTO and Entity :我有以下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
    }

But when it converts from Payment to PaymentDto I have problems with JSON decoding, specifically with the conversion from LocalDateTime to Duration .但是当它从Payment转换为PaymentDto我遇到了 JSON 解码问题,特别是从LocalDateTimeDuration的转换。 Some idea?有什么想法?

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

Thanks by the way.顺便谢谢。 ;) ;)

LocalDateTime can not be converted to Duration and vice versa. LocalDateTime不能转换为Duration ,反之亦然。 There is nothing common, except Serializable (and of course Object ), in their hierarchies.除了Serializable (当然还有Object ),在它们的层次结构中没有什么共同之处。

Replace代替

private LocalDateTime registrationDate;

with

private Duration registrationDate;

or create a new instance variable of type, Duration .或者创建一个类型为Duration的新实例变量。

As @Arvind Kumar Avinash mentioned above , you need to provide appropriate type Duration in the setter PaymentDto::setRegistrationDate .正如上面提到的@Arvind Kumar Avinash,您需要在setter PaymentDto::setRegistrationDate提供适当的类型Duration

Also you should modify the "conversion" constructor if you populate a DTO from an entity which returns a LocalDateTime field.如果您从返回LocalDateTime字段的实体填充 DTO,您也应该修改“转换”构造函数。 Also, when calculating the duration, you should place registrationDate first to avoid "negative" duration (earlier instant in time comes first).此外,在计算持续时间时,您应该首先放置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.

相关问题 JSON 解析错误:无法从字符串反序列化“java.time.LocalDateTime”类型的值 - JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String 无法从 String 反序列化 java.time.LocalDateTime 类型的值 - Can not deserialize value of type java.time.LocalDateTime from String 无法从字符串“2020-12-18 16:04:07-06”反序列化“java.time.LocalDateTime”类型的值 - Cannot deserialize value of type `java.time.LocalDateTime` from String “2020-12-18 16:04:07-06” 使用杰克逊310 +弹簧启动,得到错误“无法从字符串反序列化类型为java.time.LocalDateTime的值” - Get error “Can not deserialize value of type java.time.LocalDateTime from String” with jackson 310 + Spring boot 用Jackson反序列化String中的LocalDateTime - Deserialize LocalDateTime from String with Jackson 无法从 String 反序列化 java.sql.Timestamp 类型的值 - Cannot deserialize value of type java.sql.Timestamp from String Json InvalidFormatException:无法从字符串“flush”反序列化 HandType 类型的值 - Json InvalidFormatException : Cannot deserialize value of type HandType from String "flush" 无法从字符串反序列化类型为java.sql.Timestamp的值 - Cannot deserialize value of type `java.sql.Timestamp` from String 无法从String反序列化`java.util.Date`类型的值 - Cannot deserialize value of type `java.util.Date` from String 无法从 mockmvc 的数组值中反序列化 `java.lang.String` 类型的值 - Cannot deserialize value of type `java.lang.String` from Array value from mockmvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM