我正在使用 Spring Boot 编写Web Rest Web服务。
我想记录我的网络服务处理请求所花费的时间。我也想记录标头,方法和URI调用。
几个月前,我在jersey网络服务中做了类似的操作 ContainerRequestFilter and ContainerResponseFilter filter() method.
ContainerRequestFilter and ContainerResponseFilter filter() method.
另外,AOP是更好还是过滤器?
您是否尝试过像这样的基本过滤器?
import java.io.IOException; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component @WebFilter("/*") public class StatsFilter implements Filter { private static final Logger LOGGER = LoggerFactory.getLogger(StatsFilter.class); @Override public void init(FilterConfig filterConfig) throws ServletException { // empty } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { long time = System.currentTimeMillis(); try { chain.doFilter(req, resp); } finally { time = System.currentTimeMillis() - time; LOGGER.trace("{}: {} ms ", ((HttpServletRequest) req).getRequestURI(), time); } } @Override public void destroy() { // empty } }