我已经使用Jetty作为服务器编写了一个应用程序。我希望将我的应用程序打包成.jar
,以便稍后将其放入Docker容器中。
当我通过IntelliJ运行我的应用程序时,一切似乎都正常工作,我可以从index.html
到达localhost:2222
。但是,当我打包我的应用程序(使用mvn clean compile assembly:single
)并通过在shell中键入java -jar myapplication.jar
来运行我的jar时,index.html
上的localhost:2222
会返回HTTP Error 404
。< / p>
我的App.java
:
public class App {
public static void main( String[] args ) throws Exception {
ResourceConfig config = new ResourceConfig();
config.packages("mypackage");
ServletHolder servlet = new ServletHolder(new ServletContainer(config));
Server server = new Server(2222);
ResourceHandler handler = new ResourceHandler();
handler.setBaseResource(org.eclipse.jetty.util.resource.Resource
.newClassPathResource("index.html"));
ServletContextHandler context = new ServletContextHandler(server, "/*");
context.addServlet(servlet, "/*");
HandlerList handlers = new HandlerList();
handlers.addHandler(handler);
handlers.addHandler(context);
server.setHandler(handlers);
try {
server.start();
server.join();
} finally {
server.destroy();
}
}
}
当在IntelliJ 和中运行应用程序作为jar时,我得到以下输出(可能感兴趣?):
2015-11-06 09:25:00.991:INFO::main: Logging initialized @279ms
2015-11-06 09:25:01.238:WARN:oejsh.ContextHandler:main: o.e.j.s.ServletContextHandler@490d6c15{/,null,null} contextPath ends with /*
2015-11-06 09:25:01.238:WARN:oejsh.ContextHandler:main: Empty contextPath
2015-11-06 09:25:01.250:INFO:oejs.Server:main: jetty-9.2.3.v20140905
Nov 06, 2015 9:25:01 AM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.7 2014-03-12 18:11:31...
2015-11-06 09:25:02.294:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@490d6c15{/,null,AVAILABLE}
2015-11-06 09:25:02.306:INFO:oejs.ServerConnector:main: Started ServerConnector@268f106e{HTTP/1.1}{0.0.0.0:2222}
2015-11-06 09:25:02.306:INFO:oejs.Server:main: Started @1688ms
我没有发布我的Jersey方法文件,因为我认为它与我的问题无关。
那么,将这个打包到能够识别我index.html
localhost:2222
的jar的魔力是什么?是IntelliJ中的一些环境设置吗?
干杯
答案 0 :(得分:0)
首先,不要混用ResourceHandler
和ServletContextHandler
,你正在重复静态内容的努力( 3次与泽西一起玩) 并且会得到不可靠的结果。
删除ResourceHandler
,设置所需的ServletContextHandler.setBaseResource()
,然后在您的上下文中添加DefaultServlet
。
// Figure out what path to serve content from
ClassLoader cl = App.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
URL f = cl.getResource("static-root/index.html");
if (f == null)
{
throw new RuntimeException("Unable to find resource directory");
}
// Resolve file to directory
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("Main Base Resource is " + webRootUri);
// Setup the basic application "context" for this application at "/"
// This is also known as the handler tree (in jetty speak)
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setBaseResource(Resource.newResource(webRootUri));
server.setHandler(context);
// Jersey setup
ResourceConfig config = new ResourceConfig();
config.packages("mypackage");
context.addServlet(new ServletHolder(new ServletContainer(config)), "/*");
// Lastly, the default servlet for root content (always needed, to satisfy servlet spec)
// It is important that this is last.
ServletHolder holderDef = new ServletHolder("default",DefaultServlet.class);
holderDef.setInitParameter("dirAllowed","true");
context.addServlet(holderDef,"/");
注意:由于你在url-pattern
/*
设置Jersey,你应该知道Jersey负责提供所有静态内容,而不是Jetty。