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.Servlet...
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 {

    }

}
  • 发表于 2020-01-08 17:40
  • 阅读 ( 143 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除