兄弟项目中的 Jetty 资源库

Jetty resource base in a sibling project

我需要配置我的 Jetty 服务器,以便它可以提供来自一个项目的静态 HTML 文件和来自第二个项目的 REST 响应,但是我似乎只能让 REST 端工作...

我的项目结构类似于:

+-project-core
|    +-src/main/java/jetty/JettyRunner.java
|
+-project-web
|    +-src/main/webapp/static/index.html
|
+-project-REST
     +-src/main/java/resource/RestResource.java

我的 JettyRunner 设置服务器如下:

    // REST (JERSEY 2.0 stuff)
    WebAppContext restHandler = new WebAppContext();
    restHandler.setResourceBase("./");
    restHandler.setParentLoaderPriority(true);
    restHandler.setClassLoader(Thread.currentThread().getContextClassLoader());

    ServletHolder restServlet = restHandler.addServlet(ServletContainer.class,  "/rest/*");
    restServlet.setInitOrder(0);
    restServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "resource");
    restServlet.setInitParameter("useFileMappedBuffer", "false");

    // Web - note I can't use a ResourceHandler because it doesn't allow the init param I require below
    WebAppContext webHandler = new WebAppContext();
    webHandler.setResourceBase("../project-web/src/main/webapp/static");
    webHandler.setInitParameter("useFileMappedBuffer", "false");
    webHandler.setWelcomeFiles(new String[]{"index.html"});

    // Server
    HandlerCollection handlers = new HandlerCollection();
    handlers.addHandler(webHandler);
    handlers.addHandler(restHandler);

    Server server = new Server(8080);
    server.setHandler(handlers);
    server.start();

REST 方面似乎工作正常,我可以点击 localhost:8080/rest/someendpoint which returns a 200 但是当我点击 localhost:8080 时我得到一个目录结构project-core 而不是我期待的 index.html 文件。如果我点击 localhost:8080/index.html,我会得到一个 404。我做错了什么?

已在 Serving Static Files from Alternate Path in Embedded Jetty 中回答。

您不需要另一个 WebAppContext,只需在原来的 WebAppContext 上添加另一个特定的 DefaultServlet

Note: WebAppContext.addServlet() does exist. WebAppContext extends ServletContextHandler as seen in the other answer.