Spring MVC代码实例系列( 四 )


* @author 韩超@bksx 2018/1/25 11:17*/@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {//如果是方法请求,则进行统计if (handler instanceof HandlerMethod){//获取方法用时Long now = System.currentTimeMillis();Long useTime = now - startTime.get();//格式化当前日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String endTime = sdf.format(new Date(now));//获取URIString uri = request.getRequestURI();//获取请求类型 POST 、PUT 、GET等等String type = request.getMethod();//组合信息StringBuffer sb = new StringBuffer();sb.append("========{").append(endTime).append(",URI = [").append(uri).append("],method = ").append(true).append(",").append("Use Time : ").append(useTime).append(" 毫秒}\n");LOGGER.info(sb.toString());}}}
3.4.-mvc-.xml

3.5.
三种类型的请求日志:无参数、GET请求、POST请求
2018-01-26 15:37:12 INFORequestInfoHandlerInterceptor:75 - --------------------------------------------------------------------------------2018-01-26 15:37:12------------------------------------------------------------------------------------------Controller:pers.hanchao.hespringmvc.interceptors.LoginController----------Method:[public java.lang.String pers.hanchao.hespringmvc.interceptors.LoginController.login(javax.servlet.http.HttpServletRequest,pers.hanchao.hespringmvc.interceptors.User,org.springframework.ui.Model)----------Parameters:----------URI:[/login-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2018-01-26 15:37:12 INFORequestInfoHandlerInterceptor:103 - ========{2018-01-26 15:37:12,URI = [/login],method = true,Use Time : 3 毫秒}2018-01-26 15:37:18 INFORequestInfoHandlerInterceptor:75 - --------------------------------------------------------------------------------2018-01-26 15:37:18------------------------------------------------------------------------------------------Controller:pers.hanchao.hespringmvc.requestannotation.RequestAnnotationController----------Method:[public java.lang.String pers.hanchao.hespringmvc.requestannotation.RequestAnnotationController.postRequestParam(java.lang.String,org.springframework.ui.Model)----------Parameters:postname=%E6%9D%8E%E5%9B%9B----------URI:[/requestannotation/postrequestparam-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2018-01-26 15:37:18 INFORequestInfoHandlerInterceptor:103 - ========{2018-01-26 15:37:18,URI = [/requestannotation/postrequestparam],method = true,Use Time : 5 毫秒}2018-01-26 15:37:42 INFORequestInfoHandlerInterceptor:75 - --------------------------------------------------------------------------------2018-01-26 15:37:42------------------------------------------------------------------------------------------Controller:pers.hanchao.hespringmvc.requestannotation.RequestAnnotationController----------Method:[public pers.hanchao.hespringmvc.requestannotation.User pers.hanchao.hespringmvc.requestannotation.RequestAnnotationController.postRequestBody(pers.hanchao.hespringmvc.requestannotation.User)----------Parameters:{"name":"张三","sex":"男"}----------URI:[/requestannotation/requestbody-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2018-01-26 15:37:42 INFORequestInfoHandlerInterceptor:103 - ========{2018-01-26 15:37:42,URI = [/requestannotation/requestbody],method = true,Use Time : 20 毫秒}