Spring MVC代码实例系列( 三 )


* @author hanchao 2018/1/26 14:54**/@Overridepublic BufferedReader getReader(){return new BufferedReader(new InputStreamReader(getInputStream()));}}
3.2.对每次请求进行包装-lter
提供了对的body数据进行包装的包装器,还需要通过过滤器实现对每一个请求的包装 。lter继承了,包装每个请求只包装一次 。
package pers.hanchao.hespringmvc.interceptors.filter;import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/*** 为了包装request而创建的过滤器,实现了request.getInputStream的多次使用* OncePerRequestFilter:spring封装的Filter,包装了每个request只被调用一次* Created by 韩超 on 2018/1/25.*/public class RequestBodyWrapperFilter extends OncePerRequestFilter {@Overrideprotected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {RequestBodyWrapper wrapperedRequest = new RequestBodyWrapper(httpServletRequest);filterChain.doFilter(wrapperedRequest,httpServletResponse);}}
3.3.请求信息打印拦截器-
package pers.hanchao.hespringmvc.interceptors.interceptor;import org.apache.log4j.Logger;import org.springframework.lang.Nullable;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.BufferedReader;import java.io.InputStreamReader;import java.lang.reflect.Method;import java.text.SimpleDateFormat;import java.util.Date;/*** 请求信息拦截器:获取Controller名、method名、请求参数、URI、耗时等信息 。* 此拦截器需要HttpServletRequestBodyWrapper和HttpServletRequestBodyWrapperFilter的配合* Created by 韩超 on 2018/1/25.*/public class RequestInfoHandlerInterceptor extends HandlerInterceptorAdapter {private final static Logger LOGGER = Logger.getLogger(RequestInfoHandlerInterceptor.class);private ThreadLocal startTime = new ThreadLocal();/*** Title: 打印请求的控制器、方法、请求类型、请求参数、请求时间等信息
* @author 韩超@bksx 2018/1/25 10:49*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//如果是方法请求,则进行统计if (handler instanceof HandlerMethod){//获取方法执行前时间Long now = System.currentTimeMillis();startTime.set(now);//格式化当前时间,以便输出SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String methodTime = sdf.format(new Date(now));//获取ControllerString controller = ((HandlerMethod) handler).getBean().getClass().getName();//获取方法Method mehtod = ((HandlerMethod) handler).getMethod();//获取URIString uri = request.getRequestURI();//获取请求类型 POST 、PUT 、GET等等String type = request.getMethod();//获取请求参数//将request的body信息放到缓存读取器中BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));//创建缓存字符串存储request的body信息StringBuffer stringBuffer = new StringBuffer();//将bufferedReader的数据读取到stringBuffer中String line;while (null != (line = bufferedReader.readLine())){stringBuffer.append(line);}String parameters = stringBuffer.toString();//组合信息StringBuffer sb = new StringBuffer();sb.append("\n--------------------------------------------------------------------------------");sb.append(methodTime);sb.append("--------------------------------------------------------------------------------");sb.append("\n----------Controller:").append(controller);sb.append("\n----------Method:[").append(mehtod);sb.append("\n----------Parameters:").append(parameters);sb.append("\n----------URI:[").append(uri);sb.append("\n--------------------------------------------------------------------------------");sb.append("-------------------");sb.append("--------------------------------------------------------------------------------");LOGGER.info(sb.toString());}return true;}/*** Title:在Handler完成 handle之后,获取此时的系统时间,计算出整个handle用时