Spring @GetMapping with @RequestParam and @RequestBody fails with HttpMessageNotReadableException

Ilya Buziuk picture Ilya Buziuk · Feb 15, 2017 · Viewed 18.6k times · Source

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

Answer

Kedar Joshi picture Kedar Joshi · Feb 15, 2017

@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.