我有个拦截器,在进入我的controller之前,我读取了request的body,原始的 request是不可重复读的,于是决定使用ContentCachingRequestWrapper包装一下,但是在使用@RequestBody接收参数的时候后端报错,日志如下:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing
前端响应为
{
"timestamp": "2021-07-21T11:53:55.637+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request body is missing: public java.lang.String com.example.demo.controller.HelloController.post(com.example.demo.model.PostVO,javax.servlet.http.HttpServletRequest)",
"path": "/post"
}
这不对呀,我都已经包装成可重复读的了呀,google一顿,StackOverflow没找到解决方案,但是
这里找到了说明,说是ContentCachingRequestWrapper没有重载HttpServletRequestWrapper的getInputStream方法,当用RequestBody接收参数的时候会使用这个方法,于是不再使用ContentCachingRequestWrapper,自己重写了HttpServletRequestWrapper