我想要做的是构建一个包含我的项目的可执行JAR
文件。我已经在它旁边包含了它的依赖项,也包含在JAR
文件中,所以我的目录列表看起来像这样:
〜/项目/爪哇/ 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);
}
}
我可以验证“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
并且可以访问(我读过它)。
但是,当我启动应用程序的main方法并启动Jetty时,在http://localhost:8089
上我得到以下内容:
HTTP错误:503
访问/的问题。原因:
Service Unavailable
由Jetty提供://
我哪里错了?
我知道通过将“resourceBase”设置为“。”地址“http://localhost:8089
”将充当位置“~/Projects/Java/web-app/out/
”的界面,在该位置我可以看到所有JAR
文件的列表,包括“web-app.jar
”,点击我可以下载它。
我见过以下问题,答案不适用:
Start.class.getProtectionDomain().getCodeSource()
解析为null
后,我收到NullPointerException。我认为我应该以某种方式启用Jetty访问/webapp
文件夹,该文件夹位于我的src/main/resources/
目录下并捆绑到我的Web应用程序中。我是否应该放弃捆绑的Web应用程序并将展开的上下文路径部署到Jetty可访问的某个地方(这根本不可取,因为它给我和我的客户带来了很多问题)。
答案 0 :(得分:1)
我不确定,Jetty的WebAppContext
是否可以与PathMatchingResourcePatternResolver
一起使用!
在我的一个项目中,我通过编写自己的javax.servlet.Filter
解决了这个问题,Class#getResourceAsStream
通过Class#getResourceAsStream
加载请求的资源。
<?php
$postdata = file_get_contents("php://input");
$postdata_encoded = json_encode($_POST['message-headers']);
从jar内部以及资源路径(例如maven)内部读取资源。
希望这个提示对你有所帮助,干杯 蒂洛
答案 1 :(得分:0)
我相信,几个月前我遇到了类似的问题,我使用了Jetty v9.4.12.v20180830。
这是我的解决方案:
如果具有以下生成的JAR结构:
app.jar
+----- com /
| +--- ( classes ... )
|
+----- config /
| |
| +--- display.properties
|
+----- webapp /
|
+--- index.html
然后下面的代码将引用正确的文件夹:
ServletContextHandler webappContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
URL webRootLocation = YourJettyStarterClass.class.getResource("/webapp/index.html");
URI webRootUri = URI.create(webRootLocation.toURI().toASCIIString().replaceFirst("/index.html$", "/"));
webappContext.setContextPath("/");
webappContext.setBaseResource(Resource.newResource(webRootUri));
webappContext.setWelcomeFiles(new String[] { "index.html" });
重要部分:
URL webRootLocation = YourJettyStarterClass.class.getResource("/webapp/index.html");