【问题标题】:Embedded Jetty does not find Annotated ServletEmbedded Jetty 找不到带注释的 Servlet
【发布时间】:2014-10-29 17:14:18
【问题描述】:

短: 我有一个提供战争工件的项目,其中包括一个带有注释但没有 web.xml 的 servlet。如果我尝试在码头中使用战争,我总是只会得到战争内容的目录列表,而不是 servlet 执行。

有什么想法吗?

长篇大论: 我的 servlet 看起来像这样

package swa;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet( asyncSupported = false, urlPatterns={"/*"})
public class ServletX extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html");

        // Actual logic goes here.
        PrintWriter out = response.getWriter();
        out.println("<h1>Hi there..</h1>");
    }

}

所以我猜没什么特别的。当我使用mvn jetty:run 时,一切都很好。在确保这一点后,项目被打包到一个战争档案中。

这个战争档案在另一个必须在代码中设置码头的项目中使用。它是这样完成的:

        String jettyPort = properties.getProperty("jetty.port", "8080");
        Server server = new Server();

        ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory());
        httpConnector.setPort(Integer.parseInt(jettyPort));
        httpConnector.setAcceptQueueSize(2);
        httpConnector.setHost("0.0.0.0");
        httpConnector.setIdleTimeout(30000);
        server.setConnectors(new Connector[] { httpConnector });

        WebAppContext wacHandler = new WebAppContext();
        wacHandler.setContextPath("/admin");
        wacHandler.setWar("swa-0.0.1-SNAPSHOT.war");
        wacHandler.setConfigurationDiscovered(true);

        server.setHandler(wacHandler);

        server.start();

当执行这个项目时,日志告诉我发现了战争。但是如果我打开 url http://localhost:8080/admin 我只会看到战争内容的列表(而不是“你好”)。

有人可以指出我的失败吗?

【问题讨论】:

    标签: java servlets annotations jetty embedded-jetty


    【解决方案1】:

    更新 - 2021 年 8 月

    从 Jetty 10.0.0(包括 Jetty 11.0.0)开始,这个过程发生了变化

    类路径中jetty-annotations-&lt;ver&gt;.jar 的存在足以为您的servlet 和websocket 层启用注释和字节码扫描。

    不应再使用WebAppContext.setConfiguration(...) 方法。

    旧示例项目已归档并替换为

    https://github.com/jetty-project/embedded-servlet-server

    有……的例子

    Servlet API Version Jetty Version New Branch
    3.1 Jetty 9.4.x embedded-servlet-server : jetty-9.4.x
    4.0 Jetty 10.x embedded-servlet-server : jetty-10.0.x
    5.0 Jetty 11.x embedded-servlet-server : jetty-11.0.x

    原始答案 - 2014 年 9 月

    您需要适当地定义 WebAppContext 配置(并以正确的顺序)。

        wacHandler.setConfigurations(new Configuration[]
        { 
            new AnnotationConfiguration(), 
            new WebInfConfiguration(), 
            new WebXmlConfiguration(), 
            new MetaInfConfiguration(), 
            new FragmentConfiguration(),
            new EnvConfiguration(), 
            new PlusConfiguration(), 
            new JettyWebXmlConfiguration() 
        });
    

    别忘了添加jetty-annotations.jar

    这是来自EmbedMe.java 示例,用于 与 Servlet 3.1 一起使用,位于

    https://github.com/jetty-project/embedded-servlet-3.1/

    【讨论】:

    • @JoseMartinez 问题是关于嵌入式码头,在这种情况下使用码头 xml 并不是非常合适。现在,如果使用 jetty-distribution,此配置列表将根据您配置的模块为您管理(请参阅 jetty.home、jetty.base 和 start.ini 文档)
    • @JoakimErdfelt 是否可以在没有战争文件的情况下执行此操作?
    • @niklabaz 注释扫描是 Servlet 规范 WebApps 的一项功能,如果您没有 WAR(或展开/扩展的 webapp 目录),则它不是 WebApp,因此无需扫描。 (技术原因:这是一个 webapp 元数据和 servlet 生命周期 + 排序问题。没有正式的 webapp,不存在)
    【解决方案2】:

    除了添加上述答案中所述的必要配置外,还需要强制 Jetty 扫描当前项目编译的类,Jetty 默认会忽略这些类。为此,只需在您的 WebAppContext 上调用以下命令:

    context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/classes/.*")

    请在此处查看完整示例(在 Kotlin 中):https://github.com/mvysny/vaadin-on-kotlin/blob/master/vok-example-crud/src/test/java/com/github/vok/example/crud/Server.kt

    这很棒,可以发现所有@WebServlets、@WebListeners 和其他。

    【讨论】:

    • org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern 用于扫描服务器类加载器上的服务器容器 jar。这不适用于基于WebAppContext 的类和罐子(如WEB-INF/lib
    猜你喜欢
    • 2012-12-06
    • 2015-02-24
    • 1970-01-01
    • 2016-03-03
    • 2012-12-15
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多