1

I am using Spring Boot 2.4.5 and try to request a REST API of another application using WebClient. I know that the other application provides the requested information as a Collection. When I use an Object[] to receive the response:

    Object[] ob = (Object[]) webClient
      .get()
      .uri(endpoint)
//      .bodyValue(criteria)
      .exchangeToMono(response -> {
        if (response.statusCode()
          .equals(HttpStatus.OK)) {
          return response.bodyToMono(Object[].class);
        } else if (response.statusCode()
          .is4xxClientError()) {
          return Mono.just("Error response");
        } else {
          return response.createException()
            .flatMap(Mono::error);
        }
      }).block();

I can see that I receive a LinkedHashMap with all the values including a field:

date_of_declaration -> 2020-03-02T08:43:10

However, if possible, I want to let WebClient immediately convert the response into the designated DTOs...

     DeclarationDTO[] ob = (DeclarationDTO[]) webClient
      .get()
      .uri(endpoint)
//      .bodyValue(criteria)
      .exchangeToMono(response -> {
        if (response.statusCode()
          .equals(HttpStatus.OK)) {
          return response.bodyToMono(DeclarationDTO[].class);
        } else if (response.statusCode()
          .is4xxClientError()) {
          return Mono.just("Error response");
        } else {
          return response.createException()
            .flatMap(Mono::error);
        }
      }).block();

...I get an exception when a LocalDateTime object shall be deserialized.

org.springframework.core.codec.DecodingException: JSON decoding error: Cannot deserialize value of type `java.time.LocalDateTime` from String "02-03-2020 01:20:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '02-03-2020 01:20:00' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {HourOfAmPm=1, NanoOfSecond=0, SecondOfMinute=0, MicroOfSecond=0, MinuteOfHour=20, MilliOfSecond=0},ISO resolved to 2020-03-02 of type java.time.format.Parsed; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "02-03-2020 01:20:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '02-03-2020 01:20:00' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {HourOfAmPm=1, NanoOfSecond=0, SecondOfMinute=0, MicroOfSecond=0, MinuteOfHour=20, MilliOfSecond=0},ISO resolved to 2020-03-02 of type java.time.format.Parsed
 at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 1438] (through reference chain: java.lang.Object[][0]->de.xxx.myportal.api.infrastructure.dto.MyDTO["a_person"]->de.xxx.myportal.api.infrastructure.dto.APersonDTO["foobar"])

I think WebClient has an internal ObjectMapper, so maybe it is possible to modify this ObjectMapper during WebClient instantiation? ...or is there a better way to tell Webclient how to handle LocalDateTime? ...maybe a WebClient customized by a configuration or...?


I can't explain but after removing any JsonFormat annotation it works out of the box. (Weird!)

du-it
  • 2,561
  • 8
  • 42
  • 80
  • hopefully this will help: https://stackoverflow.com/a/53409514/2987755 and https://www.baeldung.com/spring-boot-formatting-json-dates – dkb Mar 29 '21 at 10:41

1 Answers1

0

This may help https://www.baeldung.com/spring-boot-formatting-json-dates

In your DeclarationDTO you can use something like:

@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
@JsonProperty("date_of_declaration")
private LocalDateTime dateOfDeclaration;
MadTyrael
  • 125
  • 2
  • 13
  • Sorry. I should have put that in my question. In the POJO/DTO I already defined this line: `@JsonFormat(pattern = "dd-MM-yyyy hh:mm:ss")` – du-it Mar 27 '21 at 17:18