Reputation: 113
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?
Upvotes: 1
Views: 2340
Reputation: 141
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?
Upvotes: 2