以下代码运行我的REST服务,但我的servlet过滤器永远不会被调用。有什么想法吗?
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);
答案 0 :(得分:7)
简而言之,以您上面的方式注册ResourceConfig将有效地绕过Grizzly Servlet容器。
为了利用Servlet过滤器,您需要这样的东西:
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();