Jetty 11 未检测到 Jakarta Servlet

2024-03-02

这是后续this https://stackoverflow.com/questions/66361859/jetty-11-doesnt-detect-servlets问题。我不认为这是重复的,因为接受的答案表明 Jetty 11 不能与javaxservlet,但我问为什么 Jetty 11 不能与jakarta小服务程序。

我有一个示例项目here https://github.com/KevinWorkman/HappyCoding/tree/gh-pages/examples/google-cloud/google-cloud-example-projects/app-engine-hello-world使用Jetty 9部署本地服务器,包括javaxservlet 使用@WebServlet注解。这一切都很好。

现在我尝试更新它以使用 Jetty 11 和 Jakarta servlet API。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>io.happycoding</groupId>
  <artifactId>jetty-hello-world</artifactId>
  <version>1</version>

  <properties>
    <!-- Java 11 -->
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty.version>11.0.5</jetty.version>
    <exec.mainClass>io.happycoding.ServerMain</exec.mainClass>
  </properties>

  <dependencies>
    <!-- Jakarta Servlets API -->
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>5.0.0</version>
      <scope>provided</scope>
    </dependency>

    <!-- Jetty -->
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jetty.version}</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-annotations</artifactId>
      <version>${jetty.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- Copy static resources like html files into the output jar file. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy-web-resources</id>
            <phase>compile</phase>
            <goals><goal>copy-resources</goal></goals>
            <configuration>
              <outputDirectory>
                ${project.build.directory}/classes/META-INF/resources
              </outputDirectory>
              <resources>
                <resource><directory>./src/main/webapp</directory></resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- Package everything into a single executable jar file. -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals><goal>shade</goal></goals>
            <configuration>
              <createDependencyReducedPom>false</createDependencyReducedPom>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>${exec.mainClass}</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

服务器主程序.java

package io.happycoding;

import java.net.URL;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;

/**
 * Starts up the server, including a DefaultServlet that handles static files,
 * and any servlet classes annotated with the @WebServlet annotation.
 */
public class ServerMain {

  public static void main(String[] args) throws Exception {

    // Create a server that listens on port 8080.
    Server server = new Server(8080);
    WebAppContext webAppContext = new WebAppContext();
    server.setHandler(webAppContext);

    // Load static content from inside the jar file.
    URL webAppDir =
        ServerMain.class.getClassLoader().getResource("META-INF/resources");
    webAppContext.setResourceBase(webAppDir.toURI().toString());

    // Enable annotations so the server sees classes annotated with @WebServlet.
    webAppContext.setConfigurations(new Configuration[]{ 
      new AnnotationConfiguration(),
      new WebInfConfiguration(), 
    });

    // Look for annotations in the classes directory (dev server) and in the
    // jar file (live server)
    webAppContext.setAttribute(
        "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", 
        ".*/target/classes/|.*\\.jar");

    // Handle static resources, e.g. html files.
    webAppContext.addServlet(DefaultServlet.class, "/");

    // Start the server! ????
    server.start();
    System.out.println("Server started!");

    // Keep the main thread alive while the server is running.
    server.join();
  }
}

HelloWorldServlet.java

package io.happycoding.servlets;

import java.io.IOException;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    response.setContentType("text/html;");
    response.getWriter().println("<h1>Hello world!</h1>");
  }
}

索引.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Jetty Hello World</title>
  </head>
  <body>
    <h1>Jetty Hello World</h1>
    <p>This is a sample HTML file. Click <a href="/hello">here</a> to see content served from a servlet.</p>
    <p>Learn more at <a href="https://happycoding.io">HappyCoding.io</a>.</p>
  </body>
</html>

我可以使用运行服务器mvn package exec:java它启动得很好。

My index.html文件正确加载:

但是当我导航到 servlet 的 URL 时,我收到 404:

我在命令行中没有看到任何错误(或根本没有任何输出)。

我还应该提到,我的 Jakarta servlet 可以与 Jetty 11 的完整版本配合使用(使用 Jetty 主目录和 Jetty 基本目录,如所述)here https://happycoding.io/tutorials/java-server/jetty-setup)。只是这个“独立”的 Jetty 服务器启动器似乎有问题,所以我怀疑我的ServerMain.java file.

我需要更改什么才能让 Jetty 11 检测我的 Jakarta servlet?


删除配置工作,它使用错误,并且是问题的根源。

这些行,删除它们。

    // Enable annotations so the server sees classes annotated with @WebServlet.
    webAppContext.setConfigurations(new Configuration[]{ 
      new AnnotationConfiguration(),
      new WebInfConfiguration(), 
    });

从 Jetty 10 开始,你就无法管理Configuration可以这样选择。 类路径中存在具有该功能的 jar 就足够了。 (在你的情况下是jetty-annotations-<ver>.jar)

其次,你从来没有使用过setConfigurations()与那个小Configuration列表(您遗漏了许多必需的Configuration条目)。

相反,您应该使用addConfiguration(Configuration...)方法,仅针对new AnnotationConfiguration(),但前提是该特定Configuration无法自动发现,这jetty-annotation is.

提示,使用server.setDumpAfterStart(true)在你之前server.start()调用,查看服务器状态是什么,包括您的WebAppContext看起来像,并且它的Configuration条目。这是查看您的更改对服务器和上下文造成的影响的好方法。

这是一个使用 Jetty 11 的示例项目,使用@WebServlet, and WebAppContext.

https://github.com/jetty-project/embedded-servlet-server/blob/jetty-11.0.x/src/main/java/org/eclipse/jetty/demos/EmbedMe.java https://github.com/jetty-project/embedded-servlet-server/blob/jetty-11.0.x/src/main/java/org/eclipse/jetty/demos/EmbedMe.java

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jetty 11 未检测到 Jakarta Servlet 的相关文章

  • JPA:运行时如何指定类对应的表名?

    注意 我对 Java 非常熟悉 但对 Hibernate 或 JPA 还不太熟悉 还没有 我想编写一个通过 JPA 与 DB2 400 数据库通信的应用程序 现在我可以获取表中的所有条目并将它们列出到 System out 使用 MyEcl
  • 外部硬件指纹扫描仪和 Android 设备集成

    我想建立一个android像员工考勤这样的应用程序使用fingerprint scanner 我想知道 是否可以使用外部硬件设备进行指纹识别 扫描 如何将Android应用程序与外部硬件finger集成 打印扫描设备 如何从外部硬件设备获取
  • java:无法访问org.springframework.boot.SpringApplication错误的类文件

    java cannot access org springframework boot SpringApplication bad class file C Users xyz m2 repository org springframewo
  • 由于保存之前/之后的 CSV 差异而导致错误解析(Java w/ Apache Commons CSV)

    我有一个 37 列的 CSV 文件 我正在使用 Apache Commons CSV 1 2 在 Java 中解析该文件 我的设置代码如下 initialize FileReader object FileReader fileReader
  • 构建 jar 后无法运行 exe

    我制作了一个简单的实用应用程序 其中我有一个要运行的exe文件 我通过使用它来运行 Runtime getRuntime exec this getClass getResource filename exe getPath 当我从 ide
  • 用于将字符串与预定义字符混合/混淆的简单算法

    我有一个字符串如下 它的长度是10 它代表基数 36 因此包含数字和大写字母 字符串的来源是数据库生成的序列 即从 1 及以上 正在转换为基数 36 我的问题是转换为base 36转换的结果也是连续 顺序的 例如 ID 1402 gt 00
  • 为什么需要使用java.util.TimerTask的purge()?

    Timer cancel 取消任务 Timer purge 从此计时器的任务队列中删除所有已取消的任务 如果我不在这里使用 purge 会发生什么 当计时器的任务队列已满时会发生什么 除非您正在运行的计时器数量过多 否则实际计时器行为不会发
  • 使用 Morphia 配置 Spring Boot?

    我不想利用 Spring DATA MongoDB 支持 我想利用名为 Morphia 的 MongoDB ORM https github com mongodb morphia https github com mongodb morp
  • Android 中的 ImageView 拖动限制

    我在布局中有一个 ImageView 并在 ImageView 上设置 OnTouchListener 来拖动 ImageView 它工作得很好 我的问题是如何防止将 ImageView 移动到布局范围之外 这是我的代码 活动类别 publ
  • 适当支持不区分大小写的映射

    我想实现一个不区分大小写的哈希映射 这个问题本身并不新鲜 但我想添加额外的功能 但不知道要采取什么总体方向 我希望客户能够做这样的事情 boolean preserve case true Map
  • 正则表达式的替代(流畅?)界面设计

    我刚刚看到了一个巨大的 Java 正则表达式 这让我对正则表达式的一般可维护性进行了一些思考 我相信大多数人 除了一些糟糕的 Perl 贩子 都会同意正则表达式很难维护 我正在考虑如何解决这种情况 到目前为止 我最有希望的想法是使用流畅的界
  • Java 中的逻辑回归

    我们需要用 Java 进行逻辑回归 我们在 Python 中使用了这段代码http blog smellthedata com 2009 06 python logistic regression with l2 html http blo
  • 获取运行时提供的类名的 n 维数组的类

    给定一个完全限定的类名和多个维度 我想获取该类的类名 我相信我可以这样做 public Class elementType Class forName className return Array newInstance elementTy
  • Spring Boot,使用 EhCache 进行缓存

    我需要在我的应用程序中缓存一些数据 我正在考虑使用 Ehcache 我有几个问题 Ehcache需要另外一台服务器吗 我需要其他客户端来使用 Ehcache 吗 Ehcache 如何与多个实例配合使用 是否有可能使用 Ehcache 创建类
  • Maven 在 POM 中使用加密密码

    如何在 maven 3 中使用 pom xml 中的加密密码 我有一些使用对我们来说非常敏感的密码的pom 例如我们有POM要部署在Weblogic等应用程序服务器中或在数据库中运行脚本 并且我们不喜欢只按原样输入密码 我已经拥有部署工件的
  • 如何告诉杰克逊在反序列化期间忽略空对象?

    在反序列化过程中 据我理解是将JSON数据转换为Java对象的过程 我如何告诉Jackson 当它读取不包含数据的对象时 应该忽略它 我正在使用 Jackson 2 6 6 和 Spring 4 2 6 我的控制器收到的JSON数据如下 i
  • 如何将捕获的图像写入/粘贴到文档文件?

    我有一个场景 我需要捕获图像并将它们一个接一个地写入到一个word文件中 我已经编写了下面的代码 但似乎不起作用 请帮忙 Robot robot try robot new Robot BufferedImage screenShot ro
  • 如何将 JAVAX-WS 端点绑定更改为 SOAP 1.2?

    我正在使用发布测试 WS 实现Endpoint publish 用于在 Visual Studio 中使用 根据文档 http metro java net nonav 1 2 docs endpoint html默认的 SOAP 绑定是1
  • Android 布局崩溃

    I use a XWalkView https crosswalk project org 加载网页和视频查看器 https github com Bilibili ijkplayer在我的应用程序中播放实时视频 我希望 IjkVideoV
  • hibernate通过主键查询

    我想通过主键创建查询 假设我有类主键 PersonKey 属性是 name 和 id 我有 Person 类 属性是 PersonKey 地址 DOB 现在 我想通过主键搜索人员 首先 我创建 PersonKey 的实例 并将名称设置为 j

随机推荐

  • 在javascript中使用map或reduce将二维数组转换为对象

    是否可以转换 35 Bill 20 Nancy 27 Joan to Bill 35 Nancy 20 Joan 27 使用 map or reduce 方法 我可以使用以下方法转换数组 const arr 35 Bill 20 Nancy
  • Corona SDK 的本地通知 (Android)

    有没有办法在使用 Corona SDK 时实现本地通知 这是 Android 特定的 因为我们已经找到了 iOS 方向 Cheers 目前 Corona 上的本地通知仅适用于 iOS Android 版本尚未完成 完成后 我将按照 Andr
  • 将 Numpy 矩阵显示为视频

    我有一个 numpy 矩阵 其中每一行都是一张图片 我可以重塑行并使用 matplotlib pyplot 显示图像 问题是 我不想单独显示图像 我想像视频一样依次显示它们 这在Python中怎么可能呢 好吧 我不知道这是否是最好的方法 但
  • 包含(大多数)所有元素的 HTML 页面,用于样式设置

    是否有人拥有或知道包含所有元素 带有口语文本或其他内容 的 HTML 页面 我可以做一个 但我想一定有人已经这样做了 当开始一个项目时 我喜欢为链接 列表 表格等设置一些基本样式 包含所有元素的 HTML 页面将帮助我加快此过程 我很乐意创
  • 如何在 iOS 6 中启动具有特定地址的 iOS 地图应用程序?

    我有一个应用程序 允许用户启动地图应用程序 Google 或 Apple 来查看地址 我曾经这样做过 Address address self person addresses objectAtIndex 0 NSString addres
  • 访问页面时自动点击页面上的锚链接

    我之前问过这个问题 但一些专家告诉我补充一下 这可以重新加载给定的链接 但我想知道如何在 id 的帮助下单击元素 锚点 是否有任何代码在执行时会单击 id dp99 并且我希望在访问页面时执行此 javascript 这是 HTML a h
  • Cordova、iOS 和 iframe 不会加载内容,除非我允许访问 href="*"

    我有一个网络应用程序 它有一个嵌入式地图字段 它是使用 iframe 实现的https maps google com https maps google com 我正在将我们的应用程序 当前作为主屏幕图标运行 移植到 iOS 上的 Cor
  • 在 C++ 中将数据从一个线程发送到另一个线程的最快方法是什么?

    我尝试过构建一个简单的生产者 消费者程序的实验 它们在单独的线程中运行 生产者生成一些数据 消费者在另一个线程中获取它 我实现的消息传递延迟约为 100 纳秒 谁能告诉我这是否合理或者是否有更快的实现 我没有使用锁 只是简单的内存计数器 我
  • OpenGL 中的动画 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想请您分享一些关于如何在 OpenGL 应用程序中实现动画支持的想法 例如 如果我们希望在用户输入触发某些事件的情况下 在屏幕上为球
  • 增加 Kafka 消费者在单次轮询中读取的消息数量

    Kafka消费者有一个配置max poll records它控制单次调用 poll 及其返回的最大记录数默认值为 500 https kafka apache org documentation max poll records 我已将其设
  • 如何判断一个点(X,Y)是否包含在圆的弧段内(即饼图切片)?

    想象一个圆圈 想象一下一个馅饼 想象一下 尝试返回一个布尔值 该布尔值确定所提供的 X Y 参数是否包含在这些饼图块之一中 我对弧线的了解 我有 CenterX CenterY 半径 StartingAngle EndingAngle St
  • 如何加快 AWS 设备场上的 Selenium 测试速度?

    我正在使用 Python 在 AWS 设备场上进行测试 看来开始使用硒需要非常非常长的时间 这是我使用的代码 from time import time from boto3 import client from selenium impo
  • 增加 sas 内存/memsize

    您好 有一个包含大约 6000 个观测值和 250 个变量的数据集 我正在尝试使用 proc mix 在 sas 中运行混合模型 但我不断收到 错误 由于内存不足 sas 系统停止处理此步骤 我尝试了康奈尔大学的方法 http www ci
  • 为什么@RequestBody得到一个带有null属性的对象

    我有一个 springboot REST 控制器 其中包含所示的 PATCH 和 PUT 请求方法 由于某种原因 RequestBody 公司 的字段 属性作为空值出现 我缺少什么 我在前端使用 angular8 它正在执行 PATCH 调
  • 在移动网站上禁用 JavaScript

    我的网站上有一个聊天小部件 它占据了手机的整个屏幕 如何在一定宽度的设备 或手机 上禁用聊天设备
  • “可视化页面编辑器对 Windows 64 位提供实验性支持”

    我正在使用 Eclipse Luna 和 JBoss AS 7 当我在 Eclipse 中创建 JSP 页面时 出现以下错误 我将服务器更改为 Tomcat 7 但仍然遇到相同的错误 这是如何引起的以及如何解决 该可视化页面编辑器是JBos
  • 扩展 Javascript Promise 并在构造函数内解析或拒绝它

    我想用 ES6 语法扩展原生 Javascript Promise 类 并且能够在子类构造函数中调用一些异步函数 根据异步函数结果 承诺必须被拒绝或解决 然而 当then函数被称为 子类构造函数执行两次 抛出 未捕获的类型错误 Promis
  • Oracle 中删除和删除清除之间的区别

    我正在使用 Oracle 数据库 但我对删除和清除命令有点困惑 事实上对我来说两者都做同样的事情 从数据库中删除具有架构的表 这两者之间的主要区别是什么 删除表表名 删除表表名清除 通常 如果表被删除 则会将其移至回收站 从 Oracle
  • ScalaTest 可以为所有子项目生成一个 HTML 报告吗?

    我正在尝试使用 ScalaTest 使用 Scala 2 11 0 和 SBT 0 13 x 为具有许多子项目的项目生成单个 HTML 报告 为此 我将以下行放入 build sbt 中 testOptions in ThisBuild T
  • Jetty 11 未检测到 Jakarta Servlet

    这是后续this https stackoverflow com questions 66361859 jetty 11 doesnt detect servlets问题 我不认为这是重复的 因为接受的答案表明 Jetty 11 不能与ja