码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot读取Request参数的坑

时间:2018-07-28 15:25:16      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:wrapper   职责   puts   filter   存储   intercept   one   定义   ati   

【后端拿参数相关】
默认配置时,
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接口定义保持一致。

Springboot读取Request参数的坑

标签:wrapper   职责   puts   filter   存储   intercept   one   定义   ati   

原文地址:https://www.cnblogs.com/feixuefubing/p/9381731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!