Reputation: 33
I have the following DTO
and Entity
:
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
. 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. ;)
Upvotes: 0
Views: 452
Reputation: 19565
As @Arvind Kumar Avinash mentioned above, you need to provide appropriate type Duration
in the setter PaymentDto::setRegistrationDate
.
Also you should modify the "conversion" constructor if you populate a DTO from an entity which returns a LocalDateTime
field. Also, when calculating the duration, you should place registrationDate
first to avoid "negative" duration (earlier instant in time comes first).
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;
}
Upvotes: 2
Reputation: 79500
LocalDateTime
can not be converted to Duration
and vice versa. There is nothing common, except Serializable
(and of course Object
), in their hierarchies.
Replace
private LocalDateTime registrationDate;
with
private Duration registrationDate;
or create a new instance variable of type, Duration
.
Upvotes: 2