Spring拦截器

1、SpringMVC.xml配置拦截器

	
 		
 		        
			
 		 
	
2、建一个拦截器工具类,继承拦截器接口(HandlerInterceptor)

package com.warehouse.intercept;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.MDC;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.warehouse.util.DateUtil;

public class LogInterceptor implements HandlerInterceptor {

	Date startDate = null;
	
//	[%d{yyyy-MM-dd HH:mm:ss.SSS}]|%p|%X{requestURL}|%X{className}|%X{invokeMethod}|%X{methodParams}|%X{invokeTimeBegin}|%X{exceptionCode}|%X{exceptionDetail}|%X{TimeCost}  -- %m%n
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		try {
			startDate = new Date();
			if(null != request){
				MDC.put("requestURL", request.getRequestURI());
//				if(null != request.getInputStream ()){
//					InputStream  inputStream = request.getInputStream ();  
//					StringBuilder stringBuilder = new StringBuilder ();  
//					BufferedReader streamReader = new BufferedReader (new InputStreamReader (inputStream,"UTF-8"));  
//					String inputStr = null;  
//					while ((inputStr = streamReader.readLine ()) != null)  
//					stringBuilder.append (inputStr);  
//					String parmeter = stringBuilder.toString();  
//					MDC.put("methodParams", parmeter);
//				}
			}
			if(null != handler){
				HandlerMethod handlerMethod = (HandlerMethod) handler;
				Object controller = handlerMethod.getBean();
				MDC.put("className", controller.getClass().toString());
				MDC.put("invokeMethod", handlerMethod.getMethod().getName());
			}
			MDC.put("invokeTimeBegin", DateUtil.getStandardDateTime(startDate));
			MDC.put("TimeCost", String.valueOf(DateUtil.secondBetween(startDate,new Date())));
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
//			MDC.clear();
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		try {
			if(null != request){
				MDC.put("requestURL", request.getRequestURI());
//				if(null != request.getInputStream ()){
//					InputStream  inputStream = request.getInputStream ();  
//					StringBuilder stringBuilder = new StringBuilder ();  
//					BufferedReader streamReader = new BufferedReader (new InputStreamReader (inputStream,"UTF-8"));  
//					String inputStr = null;  
//					while ((inputStr = streamReader.readLine ()) != null)  
//					stringBuilder.append (inputStr);  
//					String parmeter = stringBuilder.toString();  
//					MDC.put("methodParams", parmeter);
//				}
			}
			if(null != handler){
				HandlerMethod handlerMethod = (HandlerMethod) handler;
				Object controller = handlerMethod.getBean();
				MDC.put("className", controller.getClass().toString());
				MDC.put("invokeMethod", handlerMethod.getMethod().getName());
			}
			MDC.put("invokeTimeBegin", DateUtil.getStandardDateTime(startDate));
			MDC.put("TimeCost", String.valueOf(DateUtil.secondBetween(startDate,new Date())));
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
//			MDC.clear();
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		try {
			if(null != request){
				MDC.put("requestURL", request.getRequestURI());
//				if(null != request.getInputStream ()){
//					InputStream  inputStream = request.getInputStream ();  
//					StringBuilder stringBuilder = new StringBuilder ();  
//					BufferedReader streamReader = new BufferedReader (new InputStreamReader (inputStream,"UTF-8"));  
//					String inputStr = null;   
//					while ((inputStr = streamReader.readLine ()) != null)  
//					stringBuilder.append (inputStr);  
//					String parmeter = stringBuilder.toString();  
//					MDC.put("methodParams", parmeter);
//				}
			}
			if(null != handler){
				HandlerMethod handlerMethod = (HandlerMethod) handler;
				Object controller = handlerMethod.getBean();
				MDC.put("className", controller.getClass().toString());
				MDC.put("invokeMethod", handlerMethod.getMethod().getName());
			}
			if(null != ex){
				MDC.put("exceptionCode", ex.getMessage());
				MDC.put("exceptionDetail", ex.getMessage());
			}
			MDC.put("invokeTimeBegin", DateUtil.getStandardDateTime(startDate));
			MDC.put("TimeCost", String.valueOf(DateUtil.secondBetween(startDate,new Date())));
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
//			MDC.clear();
		}
	}

}
以上功能用拦截器实现自定义日志输出格式,给MDC赋值。里面的参数字段没有实现,原因是如果请求是GET方法,可以直接通过getParameter(String param)方法读取指定参数,可读取多次; 而POST方法的参数是存储在输入流中,只能读一次,不能多次读取。可通过AOP实现该MDC功能


你可能感兴趣的:(Java知识点)