springMVC拦截器从Request中获取Json格式并解决request的请求流只能读取一次的问题
背景
在使用SSM(你问我什么叫SSM,我一拳锤爆你的狗头)做开发的时候,经常会使用@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了
1.重写HttpServletRequestWrapper方法
public class RequestWrapper extends HttpServletRequestWrapper {
private final String body;
public RequestWrapper(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 boolean isFinished() {
return false;
}
public boolean isReady() {
return false;
}
public void setReadListener(ReadListener readListener) {}
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.拦截器CommonInterceptor
public class CommonInterceptor extends HandlerInterceptorAdapter {
private final Log log = LogFactory.getLog(CommonInterceptor.class);
public CommonInterceptor() {
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
RequestWrapper myRequestWrapper = new RequestWrapper((HttpServletRequest) request);
String body = myRequestWrapper.getBody();
System.out.println("我是拦截器:"+body);
// do something
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
super.postHandle(request, response, handler, modelAndView);
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
super.afterCompletion(request, response, handler, ex);
}
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
super.afterConcurrentHandlingStarted(request, response, handler);
}
}
3.在spring-mvc.xml声明springMVC的拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.sdut.platform.core.CommonInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
4.过滤器HttpServletFilter类
public class HttpServletFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
System.out.println("我是过滤器");
if(request instanceof HttpServletRequest) {
requestWrapper = new RequestWrapper((HttpServletRequest) request);
}
if(requestWrapper == null) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
public void destroy() {
}
}
5.在web.xml中注册过滤器
<filter>
<filter-name>requestFilter</filter-name>
<filter-class>com.sdut.platform.core.HttpServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
经过测试完全可以并且不出现其他错误。
解决办法:AOP切面获取request(一般做日志管理会经常使用这种办法)
这个办法我还没做测试
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getResponse();
上一篇: CSS3 简写animation
下一篇: 汇编语言——单片机基础