Home > Software design >  Can't use LocalDateTime RequestParam in Spring, I keep getting bad request
Can't use LocalDateTime RequestParam in Spring, I keep getting bad request

Time:07-13

I have a Spring Controller with the following signature:

@GetMapping(path = "/getInfo", produces = { MediaType.APPLICATION_JSON_VALUE,
        MediaType.APPLICATION_XML_VALUE })
public MultipleOutputSchema<CaricamentoMassivoNOBlob> get(@Nullable @RequestParam String nomeUtente,
        @Nullable @RequestParam String stato,
        @Nullable @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime startTime,
        @Nullable @RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime endTime,
        Optional<Integer> page, @Nullable @RequestParam Optional<Integer> size,
        @Nullable @RequestParam String orderBy) {
       //METHOD BODY
}

I tried to call it via Postman with the following GET:

http://localhost:8180/gateway/api/v1.0.0/getInfo?page=1&size=1&startTime=2022-07-12

And i get a 400 Bad Request. I know that the problem is related to the startTime parameter, but i can't understand how to change it... I checked here and it seems correct...

Thanks in advance

CodePudding user response:

In your request

http://localhost:8180/gateway/api/v1.0.0/getInfo?page=1&size=1&startTime=2022-07-12

You are sending query parameters startTime and endTime in form YYYY-MM-dd but on controller side you are trying to parse as LocalDateTime object, but it should be parsed as LocalDate object, since you are sending it in that form. LocalDateTime object has a form YYYY-MM-ddTHH:mm:ss.

So change this:

@GetMapping(path = "/getInfo", produces = { MediaType.APPLICATION_JSON_VALUE,
        MediaType.APPLICATION_XML_VALUE })
public MultipleOutputSchema<CaricamentoMassivoNOBlob> get(@Nullable @RequestParam String nomeUtente,
        @Nullable @RequestParam String stato,
        @Nullable @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime startTime,
        @Nullable @RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime endTime,
        Optional<Integer> page, @Nullable @RequestParam Optional<Integer> size,
        @Nullable @RequestParam String orderBy) {
       //METHOD BODY
}

to this:

   @GetMapping(path = "/getInfo", produces = { MediaType.APPLICATION_JSON_VALUE,
            MediaType.APPLICATION_XML_VALUE })
    public MultipleOutputSchema<CaricamentoMassivoNOBlob> get(@Nullable @RequestParam String nomeUtente,
            @Nullable @RequestParam String stato,
            @Nullable @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startTime,
            @Nullable @RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endTime,
            Optional<Integer> page, @Nullable @RequestParam Optional<Integer> size,
            @Nullable @RequestParam String orderBy) {
           //METHOD BODY
    }

or this :

@GetMapping(path = "/getInfo", produces = {MediaType.APPLICATION_JSON_VALUE,
            MediaType.APPLICATION_XML_VALUE})
    public void get(
             @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
             @RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime
            ) {
        //METHOD BODY

        System.out.println(startTime);
        System.out.println(endTime);
    }

If you really want to parse your query params to localDateTime objects:

http://localhost:8081/getInfo?startTime=2022-07-12T14:13:12&endTime=2022-07-12T14:13:12
  • Related