Request to the endpoint fails with the following error:
400 Bad request org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing
@GetMapping
public List<SomeObject> list(@RequestParam(required = false) String parameter, @RequestBody String body, @RequestHeader("Authorization") String token) {
.....
}
if @GetMapping
would be changed to @PostMapping
everything works like a charm though.
Any ideas what the heck is going on ?
NOTE: Swagger is used for request sending, so it is quite unlikely that the error is in Curl
UPDATE:
So, it looks like Spring does not support @RequestBody
for @GetMapping
. I still can not figure out why ? @DeleteMapping
with @RequestBody
works fine and according to HTTP/1.1 GET requests could potentially contain the body - stackoverflow.com/questions/978061/http-get-with-request-body
IMO it looks a bit inconsistent to allow body in DELETE
but forbid in GET
@RequestBody
annotation binds the content sent in (POST / PUT) request body with the annotated variable. Since there is no 'body' part in GET request, spring throws HttpMessageNotReadableException to indicate the same.
As a general rule, you can only use @RequestBody
for the requests which can have 'body' content e.g. POST or PUT.
I met the similar problem today in spring-webmvc 5.1.5 and checked the library code. The HttpMessageNotReadableException
was thrown from RequestResponseBodyMethodProcessor.readWithMessageConverters
since it invoked readWithMessageConverters
and got null. There is an loop in readWithMessageConverters
searching suitable converter for the request Content-Type (for (HttpMessageConverter<?> converter : this.messageConverters)
), and because I didn't specify and Content-Type in request, it failed.
So I specified the header Content-Type: application/json
and solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With