当前位置:网站首页>getInputStream() has already been called for this request
getInputStream() has already been called for this request
2022-07-01 07:54:00 【fate急速出击】
最近在搞一套日志系统 ,里边有个模块就是记录一下用户操作记录,需要拿到请求params,返回的result,还有就是requestbody中的内容, 然后在取body的时候发现取不到,这边文章记录一下!
报错内容
getInputStream() has already been called for this request
原因
ServletRequest的getReader()和getInputStream()两个方法只能被调用一次,而且不能两个都调用。那么如果Filter中调用了一次,在Controller里面就不能再调用了
解决办法
先将 Request Body 保存,然后通过 Servlet 自带的 HttpServletRequestWrapper 类覆盖 getReader () 和getInputStream () 方法,使流从保存的 body 读取。然后再 Filter 中将 ServletRequest 替换为 MyRequestWrapper。
添加配置类
RequestWrapper.java
package top.fate.config;
import org.apache.poi.util.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/** * @auther:Wangxl * @Emile:[email protected] * @Time:2022/6/30 16:24 */
public class RequestWrapper extends HttpServletRequestWrapper {
private final byte[] body;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
body = IOUtils.toByteArray(request.getInputStream());
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@Override
public ServletInputStream getInputStream() {
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() {
return bais.read();
}
};
}
}
HttpServletRequestReplacedFilter.java
package top.fate.config;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/** * @auther:Wangxl * @Emile:[email protected] * @Time:2022/6/30 16:35 */
public class HttpServletRequestReplacedFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest servletWrapper = null;
if (servletRequest instanceof HttpServletRequest) {
servletWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
}
if (servletRequest == null) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
try {
filterChain.doFilter(servletWrapper, servletResponse);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
}
}
边栏推荐
- Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
- Li Kou daily question - day 31 -202 Happy number
- Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
- AUTOSAR learning record (1) – ECUM_ Init
- How relational databases work
- Sqlalchemy creating MySQL_ Table
- 力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
- [programming training] delete public characters (hash mapping) + team competition (greedy)
- [MySQL learning notes 26] view
- Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
猜你喜欢

Todolist classic case ①

关系数据库如何工作

I bet on performance and won the CTO of the company. I want to build Devops platform!

2022电工(中级)复训题库及答案
![[programming training] delete public characters (hash mapping) + team competition (greedy)](/img/cd/63eb9da1e8956df0763797f079b67f.png)
[programming training] delete public characters (hash mapping) + team competition (greedy)

Apple account password auto fill

Introduction to kubernetes resource objects and common commands (II)
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario

Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?

2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
随机推荐
[MySQL learning notes 25] SQL statement optimization
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
How to use layui to display the data in the database in the form of tables
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
力扣每日一题-第32天-1822.数组元素积的符号
Caesar
PWN攻防世界int_overflow
2022 tea master (intermediate) recurrent training question bank and answers
力扣每日一题-第31天-1502.判断能否形成等差数列
[R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
Vhost kick & call principle
How relational databases work
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
凸印的印刷原理及工艺介绍
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
2022电工(中级)复训题库及答案
Saving db4i depth camera pictures with MATLAB
The database is locked. Is there a solution