~/Projects/Java/web-app/out:
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)上下文运行代码.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 ERROR: 503
Problem accessing /. Reason:
06002
Powered by Jetty://
我哪里错了?
我知道通过将“resourceBase”设置为“.”地址“http:// localhost:8089”将充当“〜/ Projects / Java / web-app / out /”位置的接口,在那里我可以看到所有JAR文件的列表,包括“web- app.jar“,点击我提供下载它.
我看过以下问题,答案不适用:
> Embedded jetty application not working from jar:我从Start.class.getProtectionDomain()获取NullPointerException.getCodeSource()解析为null.
> Embedded Jetty WebAppContext FilePermission issue:这不仅没有得到回答,但情况显然不适用,因为我没有许可问题(我可以获得文件列表的事实应证明这一点).
> embedding jetty server problems:也没有答案,也不适用,因为我没有任何依赖性问题(正如我在上面的评论中所指出的那样).
我认为我应该以某种方式启用Jetty访问/ webapp文件夹,该文件夹位于我的src / main / resources /目录下并捆绑到我的Web应用程序中.我是否应该放弃捆绑的Web应用程序并将爆炸的上下文路径部署到Jetty可访问的某个地方(这根本不可取,因为它给我和我的客户带来了很多问题).
解决方法
在我的一个项目中,我通过编写自己的javax.servlet.Filter来解决这个问题,它通过Class#getResourceAsStream加载所请求的资源.
Class#getResourceAsStream从jar内部以及资源路径(例如maven)内部读取资源.
希望这个提示对你有所帮助,干杯蒂洛
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。