http请求前后监听工具

2018-03-01 11:23:31来源:oschina作者:haoyuehong人点击

分享
package com.hz1202.common;
import com.hz1202.utils.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* @Author: mol
* @Description:http请求后监听
* @Date: create in 14:57 2018/2/27
*/
@Slf4j
public class HttpInterceptor extends HandlerInterceptorAdapter{
private static final String START_TIME = "requestStartTime";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURL().toString();
Map parameterMap = request.getParameterMap();
log.info("【请求开始】url:{},params:{}",url, JsonMapper.object2String(parameterMap));
long start = System.currentTimeMillis();
request.setAttribute(START_TIME,start);
return true;
}
/**
* 正常请求后调用
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
String url = request.getRequestURL().toString();
long start = (Long) request.getAttribute(START_TIME);
long end = System.currentTimeMillis();
log.info("【请求完成】url:{},耗时:{}",url, end-start);
}
/**
*任何请求后都要调用
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String url = request.getRequestURL().toString();
Map parameterMap = request.getParameterMap();
long start = (Long) request.getAttribute(START_TIME);
long end = System.currentTimeMillis();
log.info("【请求结束】url:{},耗时:{}, param:{}",url, end-start, JsonMapper.object2String(parameterMap));
}
}

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台