泽西岛:ContainerRequestFilter 没有获得 Context ServletRequest

Jersey: ContainerRequestFilter does not get Context ServletRequest

要查看此问题的完整代码,请参阅此 github

https://github.com/mobiusinversion/web-application

我也创建了这个 Jersey Jira

https://java.net/jira/browse/JERSEY-2851

我正在使用 Jersey 2.15 开发 ContainerRequestFilter。这是一个嵌入到单个 jar 中的 Jetty 应用程序。

在 Jetty 启动器中(主要 class):

public static void main(String[] args) throws Exception {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    Server jettyServer = new Server(10005);
    jettyServer.setHandler(context);
    ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);
    jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, ServletConstants.COMPONENT_SCAN_ROOT);

    try {
        System.out.println("Starting Jetty");
        jettyServer.start();
        System.out.println("Jetty Started");
        jettyServer.join();
    } catch (Exception e) {
        System.out.println("Could not start server");
        e.printStackTrace();
    } finally {
        jettyServer.destroy();
    }
}

我有一个通过提供商提供的过滤器

@Provider
public class ExampleProvider implements DynamicFeature {

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
        ExampleFilter exampleFilter = new ExampleFilter();
        featureContext.register(exampleFilter);
    }
}

然后在过滤器中:

public class ExampleFilter implements ContainerRequestFilter {

    private static final Logger logger = LoggerFactory.getLogger(ExampleFilter.class);

    @Context
    private HttpServletRequest servletRequest;

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        logger.info("IP ADDRESS " + servletRequest.getRemoteAddr());
        // ...
    }

}

但是这会产生 NullPointerException:

Caused by: java.lang.NullPointerException: null
at com.example.filters.ExampleFilter.filter(ExampleFilter.java:29) 
at org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:131) 
at org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:67) 

我做错了什么,我该如何解决?

更新:包括 Jetty 和 Jersey 的 pom 条目

    <properties>
        <jersey.version>2.15</jersey.version>
        <jetty.version>9.2.6.v20141205</jetty.version>
    </properties>

    <!-- Jersey -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jersey-bom</artifactId>
        <version>${jersey.version}</version>
        <type>pom</type>
        <scope>compile</scope>
    </dependency>

    <!-- Jetty -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlets</artifactId>
        <version>${jetty.version}</version>
    </dependency>

错误是您ExampleRequestFilterProvider 中手动创建了 ExampleRequestLoggingFilter 的实例。依赖注入仅适用于容器本身创建和管理的实例,例如ExampleRequestFilterProvider。这解释了为什么 @Context 注入在您手动创建的实例中似乎不起作用。

一个解决方案是将注入点移动到 ExampleRequestFilterProvider,然后将其传递给 ExampleRequestLoggingFilter 的构造函数。

@Provider
public class ExampleRequestFilterProvider implements DynamicFeature {

    @Context
    private HttpServletRequest request;

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
        ExampleRequestLoggingFilter exampleRequestLoggingFilter = new ExampleRequestLoggingFilter(request);
        featureContext.register(exampleRequestLoggingFilter);
    }

}

我在这里测试了它(感谢 Git 项目!)它对我有用。

请注意,您在这里传递的不是实际的 HttpServletRequest 实例,而是一个容器管理的代理,它在每次方法调用时进一步委托给实际实例,因此这里无需担心完整性和线程安全.