简体   繁体   中英

Grizzly Embedded Server + Jersey service + Servlet filter

The following code runs my REST service but my servlet filter never gets called. Any ideas?

WebappContext webappContext = new WebappContext("grizzly web context", "");

FilterRegistration testFilterReg = webappContext.addFilter("TestFilter", TestFilter.class);
testFilterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*");

ResourceConfig rc = new ResourceConfig().register(MyResource.class);
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:8080/myapp/"), rc);
webappContext.deploy(httpServer);

In short, registering your ResourceConfig in the manner as you have done above will effectively bypass the Grizzly Servlet container.

In order to leverage the Servlet Filter, you will need to something like this:

    WebappContext webappContext = new WebappContext("grizzly web context", "");

    FilterRegistration testFilterReg = webappContext.addFilter("TestFilter", TestFilter.class);
    testFilterReg.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), "/*");

    ServletRegistration servletRegistration = webappContext.addServlet("Jersey", org.glassfish.jersey.servlet.ServletContainer.class);
    servletRegistration.addMapping("/myapp/*");
    servletRegistration.setInitParameter("jersey.config.server.provider.packages", "com.example");


    HttpServer server = HttpServer.createSimpleServer();
    webappContext.deploy(server);
    server.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM