6

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

Ilya Buziuk
  • 1,629
  • 3
  • 23
  • 37

2 Answers2

15

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

Kedar Joshi
  • 1,090
  • 7
  • 12
  • 1
    Hmmm.... but GET also can have body - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html And spring allows to use RequestBody for GetMapping – Ilya Buziuk Feb 15 '17 at 17:56
  • 1
    Take a look at this line, in the same article - **Both HTTP GET and HTTP POST can be used to execute search with body. Since not all clients support GET with body, POST is allowed as well.** GET request with a body is not correct according to HTTP standard. – Kedar Joshi Feb 15 '17 at 17:59
  • 1
    So, what is the point - Spring does not support RequestBody for GetMapping ? – Ilya Buziuk Feb 15 '17 at 18:00
  • Correct. Now you got it. – Kedar Joshi Feb 15 '17 at 18:01
  • 2
    Get request can have body according to HTTP/1.1 see -http://stackoverflow.com/questions/978061/http-get-with-request-body – Ilya Buziuk Feb 15 '17 at 18:04
  • Also, If Spring does not support RequestBody for GetMapping why there is no compilation error ? – Ilya Buziuk Feb 15 '17 at 18:07
  • 1
    There is no compilation error because it is syntactically valid Java code. – Kedar Joshi Feb 15 '17 at 18:08
  • Well, it looks like you are right that Spring does not support RequestBody for GetMapping which is weird IMO because DeleteMapping works – Ilya Buziuk Feb 15 '17 at 18:33
  • @IlyaBuziuk If you are satisfied with the explanation, please mark this as an answer. – Kedar Joshi Feb 15 '17 at 18:50
  • thank you! I will do it in a couple of days, just want to get some other opinions (if any) – Ilya Buziuk Feb 15 '17 at 18:51
  • IMO it is a kind of inconsistent that DeleteMapping is supported (for me it is antonym to GetMapping and semantics should be the same). So, could you please update your question with DeleteMapping info (it is not only PUT / POST as you have mentioned) – Ilya Buziuk Feb 15 '17 at 18:55
1

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.

ersteLicht
  • 44
  • 1
  • 3