ServletRequest的body只能被读取一次的解决,修改Request的body/content的方法
之前修改参数,现在又有要修改body,然后搜索了很久才找到一些资料,有关content只能被读取一次的资料,转载一下
ServletRequest的说明,如下:
/** * Retrieves the body of the request as binary data using * a {@link ServletInputStream}. Either this method or * {@link #getReader} may be called to read the body, not both. * * @return a {@link ServletInputStream} object containing * the body of the request * * @exception IllegalStateException if the {@link #getReader} method * has already been called for this request * * @exception IOException if an input or output exception occurred * */ public ServletInputStream getInputStream() throws IOException; /** * Retrieves the body of the request as character data using * a <code>BufferedReader</code>. The reader translates the character * data according to the character encoding used on the body. * Either this method or {@link #getInputStream} may be called to read the * body, not both. * * * @return a <code>BufferedReader</code> * containing the body of the request * * @exception UnsupportedEncodingException if the character set encoding * used is not supported and the * text cannot be decoded * * @exception IllegalStateException if {@link #getInputStream} method * has been called on this request * * @exception IOException if an input or output exception occurred * * @see #getInputStream * */ public BufferedReader getReader() throws IOException;
两个方法都注明方法只能被调用一次,由于RequestBody是流的形式读取,那么流读了一次就没有了,所以只能被调用一次。既然是因为流只能读一次的原因,那么只要将流的内容保存下来,就可以实现反复读取了。
而对于我的需求来说,我只要在这个时候修改content,那么就实现了对content的修改
首先是重新封装Request
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import jodd.JoddDefault; import jodd.io.StreamUtil; public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper { private final byte[] body; public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException { super(request); body = StreamUtil.readBytes(request.getReader(), JoddDefault.encoding); } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(getInputStream())); } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream bais = new ByteArrayInputStream(body); return new ServletInputStream() { @Override public int read() throws IOException { return bais.read(); } }; } }
然后是filter
public class HttpServletRequestReplacedFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { //Do nothing } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ServletRequest requestWrapper = null; if(request instanceof HttpServletRequest) { requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest) request); } if(null == requestWrapper) { chain.doFilter(request, response); } else { chain.doFilter(requestWrapper, response); } } @Override public void destroy() { //Do nothing } }
以上的代码不是我的原创所以我也不方便改
下面对于需要修改body的兄弟你可以在上面的基础上修改
首先修改构造方法
public BodyReaderHttpServletRequestWrapper(HttpServletRequest request,String _body) throws IOException { super(request); if (_body == null) { body = getRequestBody(request).getBytes(); }else { body = _body.getBytes(); } }
这样就可以修改body了
然后就是之前的原作者用了一个包来读取流
我上面这个构造方法没用,用的是这个方法
private String getRequestBody (final HttpServletRequest request) { HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request); StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try { InputStream inputStream = requestWrapper.getInputStream(); if (inputStream != null) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); char[] charBuffer = new char[128]; int bytesRead = -1; while ((bytesRead = bufferedReader.read(charBuffer)) != -1) { stringBuilder.append(charBuffer, 0, bytesRead); } } } catch (IOException ex) { // log.error("Error reading the request payload", ex); // throw new AuthenticationException("Error reading the request payload", ex); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException iox) { // ignore } } } return stringBuilder.toString(); }
然后郑重提醒你别在没使用新的这个封装类型的时候写什么systemout之类的输出body的语句!
因为不要忘记原始的是只能读出一次的哦~~