Springboot读取Request参数的坑

【后端拿参数相关】
默认配置时,
getInputStream()和getReader()一起使用会报错
使用两遍getInputStream(),第二遍会为空
 
当存在@RequestBody等注解时,springMVC已读取过一遍流,默认单独使用getInputStream()或getReader()都为空。
 
解决:写filter继承HttpServletRequestWrapper,缓存InputStream,覆盖getInputStream()和getReader()方法,使用ByteArrayInputStream is = new ByteArrayInputStream(body.getBytes());读取InputStream。
 
注意:springboot中,过滤器只需@Component即可生效,另外可在FilterRegistrationBean中配置路径和优先级。
 
对于拦截器,必须在InterceptorRegistry中调用addInterceptor方法。(路径可链式添加)
 
【关于流】
只能读一遍,类似管子。
只承担传输职责,而与处理和存储无关。
对于byte流而言,进行重复读取易于实现,但指针不重置,应是为了与InputStream接口定义保持一致。

posted on 2018-07-28 13:12  kurama2018  阅读(7571)  评论(0编辑  收藏  举报