背景
在使用SpringBoot2.0做开发的时候,经常会使用@RequestBody注解,这个注解是非常的好用。在请求参数传到后台的时候做一个参数检验时,使用SpringMVC的拦截器,在拦截器里把request的数据读取出来然后校验。但是在使用了拦截器的时候会出现一个问题,在拦截器读取了request的数据,在Controller里面@RequestBody注解获取Json就会失败就读取不到数据。那就是RequestBody是流的形式读取的,流读取一次就没有了!为什么使用RequestBody只能读取一遍请求数据流?
那是因为流对应的是数据,数据放在内存中,有的是部分放在内存中。read 一次标记一次当前位置(mark position),第二次read就从标记位置继续读(从内存中copy)数据。 所以这就是为什么读了一次第二次是空了。 怎么让它不为空呢?只要inputstream 中的pos 变成0就可以重写读取当前内存中的数据。javaAPI中有一个方法public void reset() 这个方法就是可以重置pos为起始位置,但是不是所有的IO读取流都可以调用该方法!ServletInputStream是不能调用reset方法,这就导致了只能调用一次getInputStream()。解决办法:重写HttpServletRequestWrapper方法
这种方法就是通过重写HttpServletRequestWrapper把request的保存下来,然后通过过滤器保存下来的request在填充进去,这样就可以多次读取request了
package com.system.comm.utils.request; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; /** * 处理request的json body信息 * @author jimmy * @date 2019年7月31日 下午4:58:14 */ public class ReqBodyParamsRequestWrapper extends HttpServletRequestWrapper { private final String body; public ReqBodyParamsRequestWrapper(HttpServletRequest request) throws IOException { super(request); StringBuilder stringBuilder = new StringBuilder(); BufferedReader bufferedReader = null; try { InputStream 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) { throw ex; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException ex) { throw ex; } } } body = stringBuilder.toString(); } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes()); ServletInputStream servletInputStream = new ServletInputStream() { 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; } }
2.过滤器ReqBodyParamsFilter类
package com.system.comm.utils.request; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class ReqBodyParamsFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ReqBodyParamsRequestWrapper requestWrapper = null; if(request instanceof HttpServletRequest) { requestWrapper = new ReqBodyParamsRequestWrapper((HttpServletRequest) request); } if(requestWrapper == null) { chain.doFilter(request, response); } else { chain.doFilter(requestWrapper, response); } } public void destroy() { } }
3.SpringBoot配置类中加载过滤器。
/** * 处理请求参数为json字符串的过滤器 * @return */ @Bean public FilterRegistrationBean<ReqBodyParamsFilter> someFilterRegistration() { FilterRegistrationBean<ReqBodyParamsFilter> registration = new FilterRegistrationBean<ReqBodyParamsFilter>(); registration.setFilter(reqBodyParamsFilter()); registration.addUrlPatterns("/*"); registration.setName("ReqBodyParamsFilter"); return registration; } /** * request json body 过滤器 * @return */ @Bean(name = "reqBodyParamsFilter") public ReqBodyParamsFilter reqBodyParamsFilter() { return new ReqBodyParamsFilter(); }
4.拦截器中使用。
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { ReqBodyParamsRequestWrapper myRequestWrapper = new ReqBodyParamsRequestWrapper((HttpServletRequest) request); String reqBodyStr = myRequestWrapper.getBody(); }完美解决问题。