java – 为JAR文件启动嵌入式jetty服务器
我想要做的是构建一个包含我的项目的可执行JAR文件.我已经将它的依赖项包含在它旁边,也在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上,我得到以下内容:
我哪里错了? 我知道通过将“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. 我认为我应该以某种方式启用Jetty访问/ webapp文件夹,该文件夹位于我的src / main / resources /目录下并捆绑到我的Web应用程序中.我是否应该放弃捆绑的Web应用程序并将爆炸的上下文路径部署到Jetty可访问的某个地方(这根本不可取,因为它给我和我的客户带来了很多问题). 解决方法
我不确定,如果Jetty的WebAppContext可以与PathMatchingResourcePatternResolver一起使用!
在我的一个项目中,我通过编写自己的javax.servlet.Filter来解决这个问题,它通过Class#getResourceAsStream加载所请求的资源. 希望这个提示对你有所帮助,干杯蒂洛 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |