Grizzly Embedded Server + Jersey service + Servlet filter

╄→尐↘猪︶ㄣ 提交于 2019-12-13 02:30:48

问题


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);

回答1:


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();


来源:https://stackoverflow.com/questions/20510277/grizzly-embedded-server-jersey-service-servlet-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!