在Jersey和嵌入式Jetty中忽略LoggingFilter

nob*_*beh 5 java jersey embedded-jetty

我正在尝试LoggingFilter在嵌入式Jetty设置中配置Jersey.使用的胶水代码如下:

ServletContainer servletContainer = new ServletContainer(application);
ServletHolder servletHolder = new ServletHolder(servletContainer);
servletHolder.setInitParameter("com.sun.jersey.config.feature.Debug", "true");
servletHolder.setInitParameter("com.sun.jersey.config.feature.Trace", "true");
servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", 
  "com.sun.jersey.api.container.filter.LoggingFilter");
servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", 
  "com.sun.jersey.api.container.filter.LoggingFilter");
Run Code Online (Sandbox Code Playgroud)

但实际上忽略了日志记录过滤器,我在控制台中看不到相关日志.我怎样才能做到这一点?在Jersey 1.x和2.x上进行了测试.

一个相关的答案描述了如何利用这个来实现web.xml.

Rya*_*art 4

ServletContainer我认为如果它不是一个彻底的错误,那么这是记录的行为的一个非常微妙的细微差别。关于 init params 主题的文档ServletContainer如下:

所有初始化参数都作为创建的 ResourceConfig 的属性添加。

答案就藏在那里。具体来说,如果 ResourceConfig 实例不是由 ServletContainer 创建的,则 servlet 初始化参数不会添加为属性,因此不会影响应用程序的配置。当您提供自己的Application实例时,就像您对 所做的那样new ServletContainer(application),初始化大致遵循以下过程:

ServletContainer您的代码使用您的实例调用以下构造函数Application

public ServletContainer(Application app) {
    this.app = app;
}
Run Code Online (Sandbox Code Playgroud)

容器将您的初始化ServletContainer作为典型生命周期的一部分Servlet

protected void init(WebConfig webConfig) throws ServletException {
    webComponent = (app == null)
            ? new InternalWebComponent()
            : new InternalWebComponent(app);
    webComponent.init(webConfig);
}
Run Code Online (Sandbox Code Playgroud)

Application您的实例将进入InternalWebComponent构造函数。AnInternalWebComponent只是 的轻微定制WebComponent,因此:

InternalWebComponent(Application app) {
    super(app);
}
Run Code Online (Sandbox Code Playgroud)

呼叫:

public WebComponent(Application app) {
    if (app == null)
        throw new IllegalArgumentException();

    if (app instanceof ResourceConfig) {
        resourceConfig = (ResourceConfig) app;
    } else {
        resourceConfig = new ApplicationAdapter(app);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于您Application直接提供了一个实例,因此ResourceConfig在该第二个分支之一中为您构建了a if。构造完成后,立即WebComponent.init()在新组件上调用(请参阅ServletContainer.init()上面的调用,我们来自哪里)。在这个调用中,init()创建文档引用的“创建的 ResourceConfig” ,但在您的情况下,它已经存在,如我们到达此处的路径所示。即,不为空,因此下面的重要行不会执行:resourceConfig

public void init(WebConfig webConfig) throws ServletException {
    ...
    if (resourceConfig == null)
        resourceConfig = createResourceConfig(config);
    ...
}
Run Code Online (Sandbox Code Playgroud)

createResourceConfig()方法(仍在 中WebComponent)如下所示:

private ResourceConfig createResourceConfig(WebConfig webConfig)
        throws ServletException {
    final Map<String, Object> props = getInitParams(webConfig);
    final ResourceConfig rc = createResourceConfig(webConfig, props);
    rc.setPropertiesAndFeatures(props);
    return rc;
}
Run Code Online (Sandbox Code Playgroud)

您可以在该调用中看到,该调用setPropertiesAndFeatures()用于将 servlet 的 init 参数复制到ResourceConfig实例中。不幸的是,这是进行该调用的唯一位置,在您的情况下,执行永远不会在这里进行,主要是因为您使用了非默认ServletContainer构造函数之一。

我预计原作者ServletContainer只使用一个无参数构造函数编写,另外两个是后来添加的,以便于与 Servlet 3.0 容器一起使用,但没有意识到引入了这种行为。否则,我希望在文档中看到一些提及。

所以,长话短说:要么使用默认ServletContainer构造函数,要么找到一种方法来自己处理这部分:

Map<String, Object> props = getInitParams(webConfig);
rc.setPropertiesAndFeatures(props);
Run Code Online (Sandbox Code Playgroud)

第一种方法可能是最简单的。例如,您Application也可以将您的类指定为初始化参数,只要没有任何要求您提前实例化它即可,例如:

servletHolder.setInitParameter("javax.ws.rs.Application", "org.foo.MyApplication");
Run Code Online (Sandbox Code Playgroud)

这样,将采用“正常”初始化路径,这意味着WebComponent将为您创建ResourceConfig并正确应用初始化参数。