简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM