Spring耗时拦截器(url,restful)

2020-01-08 16:04:33来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Spring耗时拦截器(url,restful)

import java.io.IOException;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

@Component // 表明作为spring的一个bean
public class TimeFilter implements Filter {
    

    private HttpServletRequest request;

    private HttpServletResponse response;
    
    
    @Override
    public void destroy() {
        
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
    
        this.request = (HttpServletRequest) servletRequest;
        this.response = ((HttpServletResponse) servletResponse);
        long start = new Date().getTime();
        // 过滤器主要逻辑,整个处理流程
        chain.doFilter(request, response);
        System.out.print(request.getRequestURL());
        System.out.println(" time filter 耗时:" + (new Date().getTime() - start)+"ms");
        ;
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}

原文链接:https://www.cnblogs.com/wangpeili/p/12166790.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:缓存中间件-缓存架构的实现(上)

下一篇:mybatis - 基于拦截器修改执行中的SQL语句