带有/ @WebListener ServletContextListener 的 Jersey Grizzly 容器

Jersey Grizzly Containers w/ @WebListener ServletContextListeners

我有我的 JAX-RS Web 服务 运行 grizzly-server,但我无法配置我的 @WebListener 来工作。我正在构建一个框架,所以我无法切换到 Grizzly 侦听器。

我有最新的库。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-http-servlet</artifactId>
    <version>2.3.30</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
</dependency>

这是一些代码:

@ApplicationPath("/SampleAdmin")
public class SampleAdminApplication extends ResourceConfig {
    public SampleAdminApplication() {
        packages("com.companyname.sample.sampleadmin.server.services");
    }
}

    @WebListener
    public class SampleAdminListener implements ServletContextListener {
        private static final Logger log = Logger.getLogger(SampleAdminListener.class);

        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            log.info("SampleAdminListener Initializing context.");
        }

        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
            log.info("SampleAdminListener Destroying context.");
        }
    }
    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
     * @return Grizzly HTTP server.
     */
    public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and providers
        // in com.example.rest package
        final ResourceConfig rc = new SampleAdminApplication();

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);

        WebappContext context = new WebappContext("WebappContext", "/" + webapp);
        context.addListener("com.vilabs.sample.sampleadmin.server.servlet.SampleAdminListener");
        context.addFilter("SampleAdminFilter", SampleAdminFilter.class);
        context.addFilter("SampleAdminCsrfFilter", SampleAdminCsrfFilter.class);
//        ServletRegistration registration = context.addServlet("ServletContainer", new ServletContainer(rc));
        ServletRegistration registration = context.addServlet("ServletContainer", new ServletContainer(rc));
        registration.addMapping("/*");

        context.deploy(httpServer);
        return httpServer;
    }

对 context.deploy(httpServer) 的调用抛出 IllegalStateException。

Jun 06, 2017 7:14:14 PM org.glassfish.grizzly.servlet.WebappContext deploy
SEVERE: [WebappContext] Exception deploying application.  See stack trace for details.
java.lang.RuntimeException: java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
    at org.glassfish.grizzly.servlet.WebappContext.initServlets(WebappContext.java:1833)
    at org.glassfish.grizzly.servlet.WebappContext.deploy(WebappContext.java:318)
    at com.vilabs.sample.sampleadmin.server.SampleServer.startServer(SampleServer.java:60)
    at com.vilabs.sample.sampleadmin.server.SampleServer.main(SampleServer.java:70)
Caused by: java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
    at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:274)
    at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:221)
    at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:453)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:387)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.glassfish.grizzly.servlet.WebappContext.initServlets(WebappContext.java:1831)

IllegalStateException: The resource configuration is not modifiable in this context

这是因为 createHttpServer(uri, resourceConfig) 自动启动服务器,除非将布尔标志传递给重载方法,告诉它 自动启动。

另一件事是您正在尝试创建两个应用程序。一次当您调用 createHttpServer 传递 ResourceConfig 时,另一次当您使用相同的 ResourceConfig.

创建 ServletContainer

你应该做的只是超载createHttpServer(uri)。这不会创建应用程序,也不会启动服务器。当您使用 ResourceConfig 创建 ServletContainer 并将其传递给 WebAppContext.

时,您就创建了 Jersey 应用程序

谢谢,过滤器和监听器正在工作

/**
 * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
 * @return Grizzly HTTP server.
 */
public static HttpServer startServer() {
    // create a resource config that scans for JAX-RS resources and providers
    // in com.example.rest package
    final ResourceConfig rc = new SampleAdminApplication();

// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));

WebappContext context = new WebappContext("WebappContext", "/" + webapp);
context.addListener("com.vilabs.sample.sampleadmin.server.servlet.SampleAdminListener");
context.addFilter("SampleAdminFilter", SampleAdminFilter.class);
context.addFilter("SampleAdminCsrfFilter", SampleAdminCsrfFilter.class);
ServletRegistration registration = context.addServlet("ServletContainer", new ServletContainer(rc));
registration.addMapping("/*");

context.deploy(httpServer);
return httpServer;
}