Spring @GetMapping with @RequestParam and @Request

2019-07-23 14:53发布

问题:

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

回答1:

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



回答2:

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.