接口处理时间打印

public class RequestTimeInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(RequestTimeInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();
        request.setAttribute("startTime", startTime);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        long startTime = (Long)request.getAttribute("startTime");
        request.removeAttribute("startTime");
        long executeTime = System.currentTimeMillis() - startTime;
        log.info("接口:{},处理类:{},处理耗时:{}ms",request.getRequestURI(), handler, executeTime);
    }
}

 

上一篇:Java IO流


下一篇:day9IO流