这个线程有点旧,但我在请求之前和之后都有一段时间拦截。在网上搜索了很长时间后,我终于弄清楚了这一点:
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>blah.LoggingFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>blah.LoggingFilter</param-value>
</init-param>
然后这个类:
public class LoggingFilter extends LoggingFilter implements ContainerRequestFilter {
private static final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
public static boolean verboseLogging = false;
@Override
public ContainerRequest filter(ContainerRequest arg0) {
startTime.set(System.currentTimeMillis());
return arg0;
}
@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
System.out.println(System.currentTimeMillis() - startTime.get().longValue());
StringBuilder sb = new StringBuilder();
sb.append("User:").append((request.getUserPrincipal() == null ? "unknown" : request.getUserPrincipal().getName()));
sb.append(" - Path:").append(request.getRequestUri().getPath());
//...
}
这会在开头和结尾截获请求,以便您可以放入计时器或其他任何内容。
这适用于泽西岛 1.17。不确定 2.x.