1

The following example works for a POST request, but not for GET. My goal is to let spring automatically parse LocalDate from String input (both request body and also query parameter):

    @PostMapping("/datetime")
    public Mono<String> datetimePost(@RequestBody DateTimeDto dto) {
        return Mono.just("OK");
    }
    
    static class DateTimeDto {
        private LocalDate date;
        //getter, setter
    }

Works: POST localhost:8080/datetime

{
    "date": "2022-02-02"
}

But the same does not wor with GET:

GET localhost:8080/datetime?date=2022-02-02

@GetMapping("/datetime")
public Mono<String> datetimeGet(DateTimeDto dto) {
    return Mono.just("OK);
}

Result:

{
    "timestamp": "2022-03-03T08:57:56.248+00:00",
    "status": 400,
    "message": "'date': : Parse attempt failed for value [2022-02-02]. rejectedValue: 2022-02-02"
}

org.springframework.web.bind.support.WebExchangeBindException: Validation failed for argument at index 0 in method: public reactor.core.publisher.Mono<java.lang.Object> org.example.spring.webflux.ExampleServlet.datetimeGet(org.example.spring.webflux.ExampleServlet$DateTimeDto), with 1 error(s): [Field error in object 'dateTimeDto' on field 'date': rejected value [2022-02-02]; codes [typeMismatch.dateTimeDto.date,typeMismatch.date,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [dateTimeDto.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value '2022-02-02'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2022-02-02]]] at org.springframework.web.reactive.result.method.annotation.ModelAttributeMethodArgumentResolver.lambda$null$3(ModelAttributeMethodArgumentResolver.java:139) ~[spring-webflux-5.3.16.jar:5.3.16]

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 1
    Does this answer your question? [How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime"](https://stackoverflow.com/questions/40274353/how-to-use-localdatetime-requestparam-in-spring-i-get-failed-to-convert-string) – f1sh Mar 03 '22 at 09:05
  • No, it should work out of the box with spring 2.x, but it does not... – membersound Mar 03 '22 at 09:08
  • Are you sure? "[This is because by default, Spring cannot convert String parameters to any date or time object.](https://www.baeldung.com/spring-date-parameters)" – f1sh Mar 03 '22 at 09:11
  • Yes, because the `PostMapping` service works as expected. I don't think it makes sense that there is a different just because of the method type. – membersound Mar 03 '22 at 09:13
  • Parsing of request bodies is totally different from handling request parameters. So no it isn't strange and makes perfect sense. The request body is transformed using Jackson (unless you used Gson) and the parameter binding is done using the SPring conversion service, different beasts for different purposes. – M. Deinum Mar 03 '22 at 09:39
  • I was not aware of this. But still: shouldn't spring (boot) offer a way to by default both GET + POST requests succeed, and not only one of them? – membersound Mar 03 '22 at 10:00
  • 2
    This has nothing to do with the fact that one is a GET the other a POST but with what you are sending parameters or a body. A POST with not JSON but a plain HTML form attributes would fail in the same way the GET would fail. So it doesn't have anything to do with the method but with the content you are sending. – M. Deinum Mar 03 '22 at 10:06
  • There is a huge difference between how you think an api should work and how it actually works. Your opinion on how it should work and how spring has made it work is 2 different things. – Toerktumlare Mar 03 '22 at 13:01
  • @Toerktumlare that totally true. Anyhow a framework that claims itself being optimized in "convention over configuration", that should somewhat be standardized. And I do think that's not the case for processing a GET+POST request here (as discussed: POST uses `.ISO` parsing, GET uses `.SHORT` parsing). – membersound Mar 03 '22 at 13:17
  • Well you cant blame spring for that since spring does not maintain the jackson project. So placing blame on spring for something that has two engagements is not fair – Toerktumlare Mar 03 '22 at 14:14

1 Answers1

1

So as the way GET + POST works is different, the following fixes the problem at least:

application.properties:

spring.webflux.format.date=ISO
spring.webflux.format.time=ISO
spring.webflux.format.date-time=ISO
membersound
  • 81,582
  • 193
  • 585
  • 1,120