当前位置:网站首页>getReader() has already been called for this request
getReader() has already been called for this request
2022-06-27 01:11:00 【Small target youth】
Problem site :

reason :
HttpServletRequest Of getInputStream() and getReader() Can only be read once .
because We use @RequestBody annotation , Read body Parameters ; and also Wrote interceptors , We also need to post request ,body Take out the data .
because @RequestBody It is also read in the form of stream , After a stream reading, it's gone .
Solution :
Filters take precedence over interceptors , Let's write a filter , Inside the filter Stream data copy One for use , That is to say, copy .
Just use our replicated stream data on the interceptor .
BodyWrapperFilter.java
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Author: JCccc
* @Date: 2022-6-12 10:35
* @Description:
*/
public class BodyWrapperFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if(servletRequest instanceof HttpServletRequest) {
requestWrapper = new CustomHttpServletRequestWrapper((HttpServletRequest) servletRequest);
}
if(requestWrapper == null) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
filterChain.doFilter(requestWrapper, servletResponse);
}
}
}CustomHttpServletRequestWrapper.java
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* @Author: JCccc
* @Date: 2022-6-12 10:36
* @Description: Rewrite your own RequestWrapper take out body Use it for yourself
*/
public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
private byte[] body;
public CustomHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
BufferedReader reader = request.getReader();
try (StringWriter writer = new StringWriter()) {
int read;
char[] buf = new char[1024 * 8];
while ((read = reader.read(buf)) != -1) {
writer.write(buf, 0, read);
}
this.body = writer.getBuffer().toString().getBytes();
}
}
public String getBody(){
return new String(body, StandardCharsets.UTF_8);
}
@Override
public ServletInputStream getInputStream() {
final ByteArrayInputStream byteArrayInputStream = 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 byteArrayInputStream.read();
}
};
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}
WebApplicationConfig.java
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: JCccc
* @Date: 2022-6-23 10:52
* @Description:
*/
@Configuration
public class WebApplicationConfig {
@Bean
BodyWrapperFilter getBodyWrapperFilter(){
return new BodyWrapperFilter();
}
@Bean("bodyWrapperFilter")
public FilterRegistrationBean<BodyWrapperFilter> checkUserFilter(BodyWrapperFilter bodyWrapperFilter) {
FilterRegistrationBean<BodyWrapperFilter> registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(bodyWrapperFilter);
registrationBean.addUrlPatterns("/*");
registrationBean.setOrder(1);
registrationBean.setAsyncSupported(true);
return registrationBean;
}
} And then in the interceptor , Take out if we want to body, We use it this way instead :
CustomHttpServletRequestWrapper wrapper = (CustomHttpServletRequestWrapper) request; String nowParams = wrapper.getBody();
effect :
well , That's it .
边栏推荐
- MySQL之账号管理、建库以及四大引擎+案例
- 疫情期间居家办公的总结体会 |社区征文
- ML:机器学习工程化之团队十大角色背景、职责、产出物划分之详细攻略
- MATLAB data type - character type
- Memcached foundation 2
- Count the logarithm of points that cannot reach each other in an undirected graph [classic adjacency table building +dfs Statistics - > query set optimization] [query set manual / write details]
- Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022
- Keepalived 实现 Redis AutoFailover (RedisHA)15
- LeetCode 142. 环形链表 II
- memcached基础7
猜你喜欢

BootstrapBlazor + FreeSql实战 Chart 图表使用(2)

ArcGIS 镶嵌数据集切片丢失问题处理

Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022

MATLAB data type - character type

Hid device descriptor and keyboard key value corresponding coding table in USB protocol

05 | standard design (Part 2): how to standardize the different styles of commit information, which are difficult to read?

美团:踩雷好几年,才总结出的数据治理避坑攻略

Timing mechanism of LwIP

Gaussian and Summary Stats

What are the skills and methods for slip ring installation
随机推荐
Custom MVC (imported into jar package) + difference from three-tier architecture + reflection + interview questions
USB协议中HID设备描述符以及键盘按键值对应编码表
Kept to implement redis autofailover (redisha) 11
3线spi屏幕驱动方式
Other service registration and discovery
ArcGIS 镶嵌数据集切片丢失问题处理
buuctf-pwn write-ups (6)
对象的访问机制及其他
getReader() has already been called for this request
xml学习笔记
Generate flow chart with code, and how to use markdown
解决unable to create a folder to save the sketch: mkdir sketch
XML learning notes
ESP32-SOLO开发教程,解决CONFIG_FREERTOS_UNICORE问题
Live review | Ziya &ccf TF: Discussion on software supply chain risk management technology under cloud native scenario
memcached基础7
Keepalived 实现 Redis AutoFailover (RedisHA)17
在线文本数字识别列表求和工具
Operating instructions and Q & A of cec-i China learning machine
Memcached foundation 4