0
点赞
收藏
分享

微信扫一扫

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

小飞侠熙熙 2022-03-11 阅读 91

问题场景:

postman的POST请求在body里传json格式的参数。

自定义filter过滤Controller,filter用于记录接口调用日志,filter里记录日志时,需要获取json格式的body数据,使用了

request.getReader();

 request.getInputStream()

那么Controller就报错:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

原因:body数据不能被二次读取。filter里读取后,Controller里再次使用就会入坑。

解决方法:

一种、不使用body的json格式数据,可使用params(路径里?后面的)、headers、body(form-data)。

二种、使用spring AOP面向切面编程,一般用于事务、日志、鉴权、在用户请求时做一些统一处理等等,建@Aspect类,实现@Around方法,通过取Controller的方法参数。

调用顺序:内部顺序:@Around (point.proceed();前) —— @Before —— method —— @Around (point.proceed();后)—— @After —— @AfterReturning

举报

相关推荐

0 条评论