通过.jar部署Jetty服务器.为什么无法访问索引? [英] Deploying Jetty server via .jar. Why can't I access the index?

查看:112
本文介绍了通过.jar部署Jetty服务器.为什么无法访问索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从jar文件中部署Jetty服务器.在服务器上运行jar时,它至少会到达Jetty 404页面,但无法访问index.html.

I'm trying to deploy a Jetty server from a jar file. When jar is being run on the server, it reaches the Jetty 404 page at least, but is unable to reach index.html.

我启动服务器的主类如下所示,并且在本地主机上通过IDE运行时在本地运行良好:

My main class to launch the server looks like this, and works fine locally when run through the IDE on localhost:

public static void main(String[] args) {
    Server server = new Server(8080);

    ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
    servletContextHandler.setContextPath("/");

    DefaultServlet defaultServlet = new DefaultServlet();
    ServletHolder holderPwd = new ServletHolder("default", defaultServlet);

    final URL htmlDirectory = JerseyApplication.class.getResource("/html");

    holderPwd.setInitParameter("resourceBase", htmlDirectory.getFile());

    servletContextHandler.addServlet(holderPwd, "/*");
    server.setHandler(servletContextHandler);

    ServletHolder servletHolder = servletContextHandler.addServlet(ServletContainer.class, "/api/*");
    servletHolder.setInitOrder(0);
    servletHolder.setInitParameter(
            "jersey.config.server.provider.packages",
            "com.x.y.z.parser");

    try {
        LOGGER.info("Starting server");
        server.start();
        server.join();
    }
    catch (Exception ex) {
        LOGGER.error("Server failed to start - Aborting");
        ex.printStackTrace();
    }
    finally {
        LOGGER.info("Destroying server");
        server.destroy();
    }
}

所有html内容都位于src/main/resources/html目录中.

All html stuff is in a the src/main/resources/html directory.

当我运行jar tvf jarfile.jar | grep html时,我可以看到html目录,它的内容在其中:

When I run jar tvf jarfile.jar | grep html I can see the html directory and it's contents are in there:

0 Thu Nov 01 11:48:46 UTC 2018 html/
2258 Thu Nov 01 11:48:46 UTC 2018 html/formRequest.js
871 Thu Nov 01 11:48:46 UTC 2018 html/index.html

谢谢!

推荐答案

使用从htmlDirectory获得的URL作为整个ServletContextHandler的基础资源.

Use the URL you got from htmlDirectory as the Base Resource for the entire ServletContextHandler.

有关详细信息,请参见先前的答案: https://stackoverflow.com/a/39019797/775715

See prior answer for details: https://stackoverflow.com/a/39019797/775715

final URL htmlDirectory = JerseyApplication.class.getResource("/html");

// TODO: Handle error if htmlDirectory == null

ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.setBaseResource(Resource.newResource(htmlDirectory));

DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
// holderPwd.setInitParameter("resourceBase", htmlDirectory.getFile()); <-- not needed here
servletContextHandler.addServlet(holderPwd, "/"); // NOTE: MUST be "/" not "/*"!

最后一件事,您似乎正在使用Jersey. (又名jersey.config.server.provider.packages) 确保禁用使Jersey本身提供静态内容的Jersey配置.让Jetty负责. (关于如何完成此操作,这是另一个问题,特定于泽西岛版本,并且已经在stackoverflow上提供了答案)

One last thing, you seem to be using Jersey. (aka jersey.config.server.provider.packages) Make sure you DISABLE the Jersey configurations that make Jersey serve static content itself. Let Jetty be responsible. (as to how this is done, that's another question, which is Jersey version specific and has answers already on stackoverflow)

这篇关于通过.jar部署Jetty服务器.为什么无法访问索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆