指定JAR的ResourceBase的正确URL是什么" resources / webapp"嵌入式Jetty的文件夹? [英] What is correct URL to specify ResourceBase of JAR "resources/webapp" folder for embedded Jetty?
问题描述
我们想要一个简单的嵌入式Jetty服务器,其中包含JAR文件资源文件夹中的Web资源。我们在JAR中有一些属性文件,并使用资源路径加载它们。我们希望将Jetty资源库指定为:
We want a simple embedded Jetty servelet with the web resources inside a JAR-file's resources folder. We have some properties files in the JAR and load them using a resources path. We want to specify the Jetty Resource Base to be:
- 资源/ webapp
- set:resource_handler。 setResourceBase (webapp)
- 通过正确的URL指向该资源 JAR文件。
- resources/webapp
- set: resource_handler.setResourceBase( "webapp" )
- Via the correct URL to point to that resource in the JAR file.
JAR文件中的文件夹。这是一个简单的JAR文件(不是WAR,没有框架,没有Spring,就像我们可能那样的vanilla )。初始测试继续抛出类似以下字符串的异常:
Folder in the JAR file. This is a bare bones JAR file (not WAR, no frameworks, without Spring, as vanilla as we may). Initial tests continue to throw exceptions for something like the following strings:
webPath = "jar:file:!/webapp"; //.... runs the Jetty server ... resource_handler.setResourceBase( webPath );
虽然服务器似乎运行,但结果无法找到我的index.html。 ( 更新 :)这个例子只是从Jetty嵌入式文件服务器示例。在这种情况下,要求Jetty 资源库映射到JAR文件(完整URL):
Although the server seems to run, the result fails to find my index.html. (Update:) This example is just taking from the Jetty "Embedded File Server" example. In this case, the requirement is for the Jetty Resource Base to map to the JAR file (full-URL):
- jar:file:!/ webapp / index.html,
如下:
- resource_handler.setResourceBase( jar:file:!/ webapp );
- resource_handler.setResourceBase("jar:file:!/webapp");
而不是给出的例子:
- resource_handler.setResourceBase(。);
我们希望这将浏览器URL映射为:
And we want this to map the browser URL as:
- localhost:8080 / index.html
- ...给...
对比工作的JAR路径配置文件如下。问题:应该 URL 是这样Jetty资源库可以提供我的Index.html文件吗?
For contrast the JAR path that work for config files below. The question: what should the URL be so Jetty resource base can serve-up my Index.html file?
- 资源/
- config /
- display.properties
文件是:/ config / display.properties,这在同一项目代码中使用负载资源操作。布局是这样的:
file is: "/config/display.properties" and this works in the same project code using a load resources operation. The layout is like this:
app.jar +----- com / | +--- ( classes ... ) | +----- config / | | | +--- display.properties | +----- webapp / | +--- index.html
提供一般概念。
类似问题:
- Starting up embedded jetty server for a JAR file
推荐答案
我有一个可行的解决方案 - 我正在发布的解决方法,希望这种方法能激发正确的方法。我仍然认为应该有一种方法来指定JAR中相对于JAR的文件夹。
I have a working solution - Work-around that I'm posting in the hope that this approach will inspire correct method. I still believe there ought to be a way to specify a folder inside the JAR, relative to the JAR.
无论如何这种方法有效。我用它来从JAR内部服务静态Web内容。基本上我有Java解析正在运行的JAR资源的绝对路径,并将该路径名传递给Jetty。当我这样做时,Jetty显示我的helloWorld.html,欢迎文件。
Anyway this method works. I used it to server static web content from within the JAR. Essentially I have Java resolve the absolute path to the running JAR resource and pass that path name to Jetty. When I do that Jetty displays my "helloWorld.html", welcome file.
String baseStr = "/webapp"; //... contains: helloWorld.html, login.html, etc. and folder: other/xxx.html URL baseUrl = SplitFileServerRunner.class.getResource( baseStr ); String basePath = baseUrl.toExternalForm(); .... resource_handler.setDirectoriesListed(true); //... just for testing resource_handler.setWelcomeFiles(new String[]{ "helloWorld.html" }); resource_handler.setResourceBase( basePath ); LOG.info("serving: " + resource_handler.getBaseResource());
在欢迎文件中,我已经设置了特定文本来标识文件的来源(在资源文件夹中) 。在浏览器中:
In the welcome file, I have put specific text to identify the file's origin (in the resources folder). In the browser:
- localhost:8080
提供helloWorld.html文件。
Serves the helloWorld.html file.
- localhost:8080 / other
显示JAR文件中 jar:/ webapp / other / 目录的目录列表。这依赖于不在服务器运行时更改JAR。
Shows a directory listing of the jar:/webapp/other/ directory inside the JAR file. This relies on not changing the JAR while the server is running.
在Linux上如果有人 cp -sa运行JAR顶部的新jar文件,Jetty给出:
On Linux if someone cp-s a new jarfile on-top of the running JAR, Jetty gives:
HTTP ERROR: 500 Problem accessing /. Reason: java.lang.NullPointerException
你无法访问页面了。这是出乎意料的(显然JAR保持开放)。好消息是,如果你 mv -s jarfile:
And you can't access pages any more. That was unexpected (evidently the JAR is kept open). The good news is that if you mv-s the jarfile:
- mv fileserver.jar fileserverXX.jar
Jetty很高兴继续从(重命名的)fileserverXX.jar内容提供服务。我很高兴。但是我仍然想知道匹配绝对文件名的等效相对路径。
Jetty happily continues serving from the (renamed) fileserverXX.jar content. I can be happy with that. However I'd still like to know the equivalent relative path to match the absolute file name.
这篇关于指定JAR的ResourceBase的正确URL是什么" resources / webapp"嵌入式Jetty的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- config /