Mil*_*eri 8 java web-applications jetty executable-jar embedded-jetty
我想要做的是构建一个JAR包含我的项目的可执行文件.我已经将它的依赖项包含在它旁边,也包含在JAR文件中,所以我的目录列表看起来像这样:
〜/项目/ Java的/ web应用程序输入/输出:
网络app.jar
dependency1.jar
dependency2.jar
dependency3.jar
我知道并确信我的问题不是来自依赖关系,因为我的应用程序正常运行,直到我启动Jetty嵌入式的那一刻.
我用来启动Jetty的代码是这样的:
public class ServerExecutionGoal implements ExecutionGoal {
private final static Logger logger = Logger.getLogger(ServerExecutionGoal.class);
private WebAppContext getWebAppContext() throws IOException {
WebAppContext context = new WebAppContext();
System.out.println("context = " + context);
context.setResourceBase(new PathMatchingResourcePatternResolver().getResource("classpath:/webapp").getURL().toExternalForm());
context.setContextPath("/");
context.setLogger(new StdErrLog());
return context;
}
@Override
public void execute(Map<String, Object> stringObjectMap) throws ExecutionTargetFailureException {
logger.info("Instantiating target server ...");
final Server server = new Server(8089);
final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
try {
handlerCollection.setHandlers(new Handler[]{new RequestLogHandler(), getWebAppContext()});
} catch (IOException e) {
throw new ExecutionTargetFailureException("Could not create web application context", e);
}
server.setHandler(handlerCollection);
try {
logger.info("Starting server on port 8089 ...");
server.start();
server.join();
logger.info("Server started. Waiting for requests.");
} catch (Exception e) {
throw new ExecutionTargetFailureException("Failed to properly start the web server", e);
}
}
public static void main(String[] args) throws ExecutionTargetFailureException {
new ServerExecutionGoal().execute(null);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以验证"webapp"文件夹是否在我的JAR中正确地重新定位/webapp,并且当通过我的IDE(IntelliJ IDEA 11)运行代码时,context.setResourceBase(new PathMatchingResourcePatternResolver().getResource("classpath:/webapp").getURL().toExternalForm())有效地映射到相关资源.此外,我可以证明它解决了类似于:
jar:file:~/Projects/Java/web-app/out/web-app.jar!/webapp
Run Code Online (Sandbox Code Playgroud)
并且可以访问(我读过它).
但是,当我启动应用程序的main方法,Jetty启动时,http://localhost:8089我得到以下内容:
HTTP错误:503
访问/的问题.原因:
Run Code Online (Sandbox Code Playgroud)Service Unavailable
由Jetty提供支持://
我哪里错了?
我知道通过将"resourceBase"设置为"." 地址" http://localhost:8089"将作为位置" ~/Projects/Java/web-app/out/" 的界面,在那里我可以看到所有JAR文件的列表,包括" web-app.jar",点击我提供下载它.
我看过以下问题,答案不适用:
Start.class.getProtectionDomain().getCodeSource()解析为,我得到NullPointerException null.我认为我应该以某种方式启用Jetty访问该/webapp文件夹,该文件夹位于我的src/main/resources/目录下并捆绑到我的Web应用程序中.我是否应该放弃捆绑的Web应用程序并将爆炸的上下文路径部署到Jetty可访问的某个地方(这根本不可取,因为它给我和我的客户带来了很多问题).
我不确定WebAppContextJetty 是否可以与PathMatchingResourcePatternResolver!
在我的一个项目中,我通过编写自己的 解决了这个问题javax.servlet.Filter,它通过 加载请求的资源Class#getResourceAsStream。
Class#getResourceAsStream从 jar 内部以及资源路径(例如 maven)内部读取资源。
希望这个提示对你有帮助,欢呼 Thilo
| 归档时间: |
|
| 查看次数: |
2956 次 |
| 最近记录: |