我正在尝试从 jar 文件部署 Jetty 服务器。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我可以看到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
呼啦一阵风
相关分类