我试图从部署一个jar文件中的Jetty服务器。 当罐子被在服务器上运行,它至少到达码头404页,但无法达到index.html
。
我启动服务器主类看起来是这样的,当通过在本地主机上的IDE运行本地工作正常:
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
目录。
当我运行jar tvf jarfile.jar | grep html
jar tvf jarfile.jar | grep html
我可以看到html
目录和它的内容都在里面:
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
谢谢!