JettyHttpContainerFactory和ServletContextHandler

时间:2014-09-25 17:03:58

标签: jersey jetty embedded-jetty

我正在尝试使用嵌入式Jetty和Jersey。经过一些测试后,我最终使用JettyHttpContainerFactory,但我意识到,使用这个容器 工厂我无法配置ServletContextHandler,因为服务器是 生成已使用JettyHttpContainer

有什么办法,我如何使用ServletContextHandler添加JettyHttpContainerFactory? 这是我的代码:

    URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();
    // This is what I want in addition
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
    context.callContextInitialized(listener, new ServletContextEvent(new ContextHandler.NoContext()));
    context.addEventListener(listener);
    DatabaseContextListener databaseListener = new DatabaseContextListener();
    context.addEventListener(databaseListener);
    context.setAttribute(DatabaseContextListener.DB_CONNECTION_ATTRIBUTE, databaseListener);

    FilterHolder filterHolder = new FilterHolder();
    filterHolder.setFilter(new ShiroFilter());
    EnumSet<DispatcherType> types = EnumSet.allOf(DispatcherType.class);
    context.addFilter(filterHolder, "/*", types);
    ServletHolder holder = new ServletHolder(ServletContainer.class);
    holder.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "org.vrabec.datagateway.rest");

    context.addServlet(holder, "/*");

    // This part is working as expected
    ResourceConfig configuration = new ResourceConfig().packages("org.vrabec.datagateway.rest").register(GensonCustomResolver.class);
    Server server = JettyHttpContainerFactory.createServer(baseUri, configuration, false);

    // This is what I want in addition
    server.setHandler(context);
    server.start();
    server.join();

1 个答案:

答案 0 :(得分:3)

最后我设法完成了。这是源代码:

    Server server = new Server(PORT_NUMBER);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
    context.callContextInitialized(listener, new ServletContextEvent(new ContextHandler.NoContext())); 
    context.addEventListener(listener);
    DatabaseContextListener databaseListener = new DatabaseContextListener();
    context.addEventListener(databaseListener);
    context.setAttribute(DatabaseContextListener.DB_CONNECTION_ATTRIBUTE, databaseListener);

    FilterHolder filterHolder = new FilterHolder(); 
    filterHolder.setFilter(new ShiroFilter()); 
    EnumSet<DispatcherType> types = EnumSet.allOf(DispatcherType.class); 
    context.addFilter(filterHolder, "/*", types); 

    ServletHolder holder = new ServletHolder(new ServletContainer(new ResourceConfig()
    .packages("org.vrabec.datagateway.rest")
    .register(GensonCustomResolver.class)));    
    holder.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "org.vrabec.datagateway.rest");

    context.addServlet(holder, "/*");
    server.setHandler(context);
    server.start();
    server.join();