You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetty 11无法检测Jakarta Servlet,该如何调试排查?

解决Jetty 11嵌入式场景下Servlet扫描失效导致404的问题

你的核心问题是Jetty 11未自动扫描并注册Servlet类,虽欢迎页可正常访问,但所有Servlet请求返回404。结合你的代码,以下是具体修复步骤:

1. 确保依赖齐全

如果使用Maven,必须添加Jetty注解扫描相关依赖,否则AnnotationConfiguration无法生效:

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-annotations</artifactId>
    <version>${jetty.version}</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-plus</artifactId>
    <version>${jetty.version}</version>
</dependency>

2. 修改WebAppContext配置,强制启用注解扫描

你的代码未显式添加AnnotationConfiguration,嵌入式Jetty不会自动启用该配置类。需手动配置并指定类扫描路径:

修改后的主类代码:

public static void main(String[] args) throws Exception {
    // Create a server that listens on port 8080.
    int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
    String host = System.getenv("HOST");
    Server server;
    if (host != null)
        server = new Server(new InetSocketAddress("dev.ourwines.com", port));
    else
        server = new Server(port);

    WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setWelcomeFiles(new String[]{"index.html"});
    context.setParentLoaderPriority(true);

    // 配置静态资源目录
    URL webAppDir = Main.class.getClassLoader().getResource("META-INF/resources");
    System.out.println("webAppDir " + webAppDir);
    context.setResourceBase(webAppDir.toURI().toString());

    // 关键:添加AnnotationConfiguration到配置类列表,启用注解扫描
    context.setConfigurationClasses(new String[]{
        "org.eclipse.jetty.webapp.WebInfConfiguration",
        "org.eclipse.jetty.webapp.WebXmlConfiguration",
        "org.eclipse.jetty.webapp.MetaInfConfiguration",
        "org.eclipse.jetty.webapp.FragmentConfiguration",
        "org.eclipse.jetty.annotations.AnnotationConfiguration",
        "org.eclipse.jetty.webapp.JettyWebXmlConfiguration"
    });

    // 指定Servlet类所在的包路径(替换为你的实际包名)
    context.setAttribute("org.eclipse.jetty.annotations.AnnotationConfiguration.scanPackage", "com.yourcompany.yourpackage");

    server.setHandler(context);

    // 打印配置类,确认AnnotationConfiguration已被添加
    System.out.println("Configuration discovered? " + context.isConfigurationDiscovered());
    System.out.println("getBaseResource " + context.getBaseResource());
    String[] classes = context.getConfigurationClasses();
    for (String cls : classes)
        System.out.println("getConfigurationClasses " + cls);

    // 开启启动后状态打印,方便排查
    server.setDumpAfterStart(true);
    server.start();
    server.join();
}

3. 关于日志无输出的问题

你之前设置org.eclipse.jetty.annotations.AnnotationConfiguration的DEBUG日志没效果,是因为该配置类未被加载。按上述步骤添加配置类后,再开启DEBUG日志即可看到Servlet扫描的详细过程。

4. 额外注意事项

  • 你的Servlet类必须使用**Jakarta Servlet API 5.0+**的注解(@WebServlet等),Jetty 11完全基于Jakarta EE,不再兼容旧的javax.servlet包。
  • 确保编译后的类文件在类路径下,Jetty需扫描到这些.class文件才能完成Servlet注册。

内容的提问来源于stack exchange,提问作者Kjell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 13:41:22