在到达 endpoints/resources 之前调试 Jersey mapping/routing 执行

Debug Jersey mapping/routing execution before reaching endpoints/resources

我已经使用 Glassfish/Jackson 一年多了,在引入新的端点实现时我总是遇到这个问题:当 没有达到端点时,我想了解为什么,我必须继续的唯一提示是返回的请求,因为执行 没有到达所需的端点或资源 (routing/mapping 错误)。

我想在到达endpoints/resources之前通过"raw"请求拦截Jersey mapping/routing执行,这样我可以更好地理解resource/endpoint 映射和路由问题。

This answer to a different question, by @xeye,为我解决了这个问题:

创建一个实现 ContainerRequestFilter 的过滤器,并覆盖其 filter 方法。这将是我们可以拦截所有请求以进行调试的地方。

// Executed whenever a request is sent to the API server.
// Useful for debugging errors that don't reach the desired endpoint implementation.
@Provider
@Priority(value = 0)
public class MyFilter implements ContainerRequestFilter {
    @Context // request scoped proxy
    private ResourceInfo resourceInfo;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        try {
            // put a breakpoint or log desired attributes of requestContext here.
        } catch (Exception e) {
            // ignore
        }
   }
}

然后 在您的 ConfigResource 实现中注册这个新 class

public class MyResourceConfig extends ResourceConfig {
    public MyResourceConfig(){
        register(MyFilter.class);
        // ...
    }

(It's OK to Ask and Answer Your Own Questions)