场景:
在过滤器中获取 request 所携带的参数,get可以直接由 request.getParameterMap()
获取,但无法获取 post 在 body 中
携带的请求参数。故想到用流获取
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
String body = IOUtils.readAll(reader);
可以获取到参数,但报了 HttpMessageNotReadableException ,查资料得知 @RequestBody 注解中也使用到了 request 流,查看InputStream的源码可知,读取流的时候会根据position来获取当前位置,并且随着读取来进行位置的移动。如果想要重新读取,可以调用inputstream.reset方法,但是能否reset取决于markSupported方法,返回true可以reset,反之不行。查看ServletInputStream可知,这个类并没有复写markSupported和reset方法,查看父类InputStream:
public boolean markSupported() {
return false;
}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
可知ServletInputStream不支持reset,故这个流只能读取一次。
解决方法:
重写 HttpServletRequestWrapper
HttpServletRequestWrapper 是 HttpServletRequest的包装类,可以在Wrapper实现增加一个缓存流的地方,以防止使用一次即丢失问题,过滤器中用HttpServletRequestWrapper替换HttpServletRequest,然后在chain.doFiler方法中传递新的request对象。
RequestWrapper 如下:
public class RequestWrapper extends HttpServletRequestWrapper {
private final String body;
public RequestWrapper(HttpServletRequest request) {
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
InputStream inputStream = null;
try {
inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
} finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
body = stringBuilder.toString();
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
ServletInputStream servletInputStream = new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public String getBody() {
return this.body;
}
}
Filter修改如下:
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
RequestWrapper requestWrapper = null;
requestWrapper = new RequestWrapper(request);
// post 请求无法通过 getParameter 获取请求参数,故用 stream 处理,stream 产生部分问题,由重写的 RequestWrapper 解决
if("POST".equals(method)) {
String body = requestWrapper.getBody();
// 具体处理代码
}else {
String body = JSONObject.toJSONString(request.getParameterMap());
// 具体处理代码
}
// 具体处理代码
chain.doFilter(requestWrapper, response);
}