1

I am sending a Http POST request to http://localhost:8080/date with the body

{
    "localDateTime": "2021-06-08T11:39:01"
}

(I tried multiple different time formats, always the same error) to my Java application built with Spring Boot.

@RestController
public class Controller {

@PostMapping(path ="/date",consumes="application/json",produces="application/json")
    public String dateTime(@RequestParam(name = "localDateTime") 
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localDateTime) {
        return localDateTime.toString();
    }
}

I know it's a little bit tricky with Spring Boot, but I read that the @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) annotation should allow it to properly convert the Strings to date objects.

Instead I get the 400 Bad Request error. What's wrong?

Steve450
  • 113
  • 2
  • 7

1 Answers1

2

Please, remove consumes="application/json", produces="application/json", you are already using the RestController annotation.

Change @RequestParam to @RequestBody, if you want to send JSON as Body. Use @RequestParam for /date?localDateTime=2021-06-08T11:39:01.

What is difference between @RequestBody and @RequestParam?

Mykola
  • 121
  • 2
  • 11
  • If my answer helped you, please, mark it as correct answer to help community find solutions. – Mykola Jul 05 '21 at 16:23