本文介绍了启动JAR文件的嵌入式Jetty服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是建立一个包含我的项目的可执行文件JAR.我在JAR文件中也包括了它的依赖项,因此我的目录清单如下所示:

What I am trying to do, is to build an executable JAR file which will contain my project. I have included its dependencies right next to it, also in JAR files, so my directory listing looks something like this:

web-app.jar

web-app.jar

dependency1.jar

dependency1.jar

dependency2.jar

dependency2.jar

dependency3.jar

dependency3.jar

我知道并确定我的问题不会由依赖引起,因为直到我启动Jetty嵌入式应用程序时,我的应用程序才能正常运行.

I know and am sure that my problem does not arise from dependencies, as my application functions properly, right up to the moment I start up Jetty embedded.

我用来启动Jetty的代码是这样的:

The code I use to start Jetty is like this:

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())可以有效地映射到所讨论的资源.另外,我可以证明它可以解决以下问题:

I can validate that the "webapp" folder gets relocated correctly inside my JAR to /webapp, and that when running the code through my IDE (IntelliJ IDEA 11) context.setResourceBase(new PathMatchingResourcePatternResolver().getResource("classpath:/webapp").getURL().toExternalForm()) maps validly to the resource in question. Also, I can show that it resolves to something like:

jar:file:~/Projects/Java/web-app/out/web-app.jar!/webapp

并且可以访问(我已阅读).

and is accessible (I read it).

但是,当我启动应用程序的main方法并启动Jetty时,在http://localhost:8089上我得到以下信息:

However, when I start my application's main method, and Jetty starts, on http://localhost:8089 I get the following:

问题访问/.原因:

Service Unavailable


由Jetty://


Powered by Jetty://

我要去哪里错了?

我知道将"resourceBase"设置为."地址"http://localhost:8089"将充当位置"~/Projects/Java/web-app/out/"的接口,在单击该位置后,我可以在其中看到所有JAR文件的列表,包括"web-app.jar".下载.

I know that by setting "resourceBase" to "." the address "http://localhost:8089" will act as an interface to the location "~/Projects/Java/web-app/out/" where I can see a listing of all the JAR files, including the "web-app.jar", upon clicking on which I am offered to download it.

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

I have seen the following questions, and the answers do not apply:

  1. 嵌入式码头应用程序无法从jar中运行:我收到了NullPointerException因为Start.class.getProtectionDomain().getCodeSource()解析为null.
  2. 嵌入式Jetty WebAppContext FilePermission问题:不仅没有得到回答,而且还没有得到解决这种情况显然不适用,因为我没有权限问题(我可以获取文件列表这一事实应证明这一点).
  3. 嵌入码头服务器问题:也未得到回答,也不适用,因为我没有任何依赖关系问题(如我之前在上面的评论中所指出的那样).
  1. Embedded jetty application not working from jar: I get NullPointerException since Start.class.getProtectionDomain().getCodeSource() resolves to null.
  2. Embedded Jetty WebAppContext FilePermission issue: which not only isn't answered, but the situation clearly does not apply, as I do not have permission issues (the fact that I can get a file listing should prove that).
  3. embedding jetty server problems: also unanswered, also not applicable as I don't have any dependency problems (as pointed out previously in my comments above).

我认为我应该以某种方式使Jetty能够访问/webapp文件夹,该文件夹位于我的src/main/resources/目录下,并捆绑到我的Web应用程序中.我应该抛弃捆绑的Web应用程序,而是将展开的上下文路径部署到Jetty可以访问的某个位置吗(这完全是不希望的,因为它对我和我的客户造成了很多问题).

I think that I should somehow enable Jetty to access the /webapp folder, which is located under my src/main/resources/ directory and is bundled into my web application. Should I be forsaking a bundled web application and deploy the exploded context path to somewhere accessible by Jetty instead (this is not desirable at all as it poses a multitude of issues for me and my clients).

推荐答案

我不确定Jetty的WebAppContext是否可以与PathMatchingResourcePatternResolver一起使用!

I'm not sure, if the WebAppContext of Jetty can be used with PathMatchingResourcePatternResolver!

在我的一个项目中,我编写了自己的javax.servlet.Filter解决了此问题,该文件由Class#getResourceAsStream加载了请求的资源.Class#getResourceAsStream从罐子内部以及资源路径(例如maven)中读取资源.

In one of my projects I solved this problem by writing my own javax.servlet.Filter, which loads the requested resources by Class#getResourceAsStream.Class#getResourceAsStream reads resources from inside a jars as well as inside a resource path (e.g. maven).

希望此提示对您有所帮助,干杯锡洛

Hope this tip is helpful for you, cheersThilo

这篇关于启动JAR文件的嵌入式Jetty服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:46