如何使用嵌入式码头服务 index.html?

How to serve index.html with embedded jetty?

我在下面创建了嵌入式码头服务器。它从 [host]:[port]:/api/

成功地为我的 REST API 服务器提供服务

我现在有一个 Angular 应用程序,我想在 API 之外提供服务,我该如何更新我的代码,以便服务器为该应用程序的 index.html 文件提供服务?在尝试了几种不同的方法之后,我发现我缺乏理解,我无法适应我所看到的例子。我们将不胜感激地收到任何建议或意见。谢谢!

public class JettyServer {

  private static final String API_PREFIX = "/api/*";
  private static final String RESOURCE_PACKAGES_TO_SCAN = "org.brewtraption.rest";
  Logger logger = LoggerFactory.getLogger(JettyServer.class);
  private JettyServerConfig config = null;

  public JettyServer(final JettyServerConfig config) {
    this.config = config;
  }

  public void start() {
    InetSocketAddress socket = new InetSocketAddress(config.getHost(), config.getPort());
    Server server = new Server(socket);
    ServletHolder servletHolder = configureAPIResources();

    ServletContextHandler context = configureServletContextHandler(server, servletHolder);
    initaliseWebSocketContainer(context);

    startBrewtraptionServer(server);
  }

  private void startBrewtraptionServer(Server server) {
    try {
      server.start();
      server.join();
    } catch (Exception e) {
      String message = "Unable to start Brewtraption Server. %s %s";
      logger.error(String.format(message, e.getClass().getName(), e.getMessage()));
    }
  }

  private void initaliseWebSocketContainer(ServletContextHandler context) {
    try {
      ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
      container.addEndpoint(EventSocket.class);
    } catch (ServletException | DeploymentException e) {
      String message = "Unable to create ServerContainer. %s %s";
      logger.error(String.format(message, e.getClass().getName(), e.getMessage()));
    }
  }

  private ServletContextHandler configureServletContextHandler(Server server, ServletHolder servletHolder) {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addServlet(servletHolder, API_PREFIX);

    FilterHolder filterHolder = new FilterHolder(CrossOriginFilter.class);
    filterHolder.setInitParameter("allowedOrigins", "*");
    filterHolder.setInitParameter("allowedMethods", "GET, POST, PUT");
    context.addFilter(filterHolder, "/*", null);

    server.setHandler(context);
    return context;
  }

  private ServletHolder configureAPIResources() {
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig = resourceConfig.packages(RESOURCE_PACKAGES_TO_SCAN)
      .register(JacksonFeature.class);
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    return new ServletHolder(servletContainer);
  }
}

你需要两样东西。

  1. 一个默认Servlet
  2. 一个已配置的欢迎文件。
    context.setWelcomeFiles(new String[] { "index.html", "index.htm", "index.jsp" });

    // Lastly, the default servlet for root content (always needed, to satisfy servlet spec)
    // It is important that this is last.
    ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
    holderPwd.setInitParameter("resourceBase",pwdPath);
    holderPwd.setInitParameter("dirAllowed","true");
    context.addServlet(holderPwd,"/");