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

后端 未结 2 1036
醉酒成梦
醉酒成梦 2020-12-11 19:43

Request to the endpoint fails with the following error:

400 Bad request org.springframework.http.converter.HttpMessageNotReadableException: Requir

相关标签:
2条回答
  • 2020-12-11 20:30

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

    0 讨论(0)
  • 2020-12-11 20:48

    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.

    0 讨论(0)
提交回复
热议问题