首页 文章

为JAR文件启动嵌入式jetty服务器

提问于
浏览
7

我想要做的是构建一个可执行的 JAR 文件,它将包含我的项目 . 我已将它的依赖项包含在它旁边,也在 JAR 文件中,所以我的目录列表看起来像这样:

〜/ 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)运行代码时, 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访问/时出现问题 . 原因:服务不可用
由Jetty提供支持://

我哪里错了?

我知道通过将"resourceBase"设置为"."地址“ http://localhost:8089 " will act as an interface to the location " ~/Projects/Java/web-app/out/ ”,我可以看到所有 JAR 文件的列表,包括“ web-app.jar ”,点击我可以下载的地址 .

我看过以下问题,答案不适用:

我认为我应该以某种方式启用Jetty访问 /webapp 文件夹,该文件夹位于我的 src/main/resources/ 目录下并捆绑到我的Web应用程序中 . 我是否应该放弃捆绑的Web应用程序并将爆炸的上下文路径部署到Jetty可访问的某个地方(这根本不可取,因为它给我和我的客户带来了很多问题) .

1 回答

  • 0

    我不确定,如果Jetty的 WebAppContext 可以和 PathMatchingResourcePatternResolver 一起使用!

    在我的一个项目中,我通过编写自己的 javax.servlet.Filter 解决了这个问题,它通过 Class#getResourceAsStream 加载所请求的资源 . Class#getResourceAsStream 从jar内部以及资源路径(例如maven)内部读取资源 .

    希望这个提示对你有所帮助,欢呼Thilo

相关问题