![](/img/trans.png)
[英]java.lang.ClassNotFoundException: jakarta.servlet.http.HttpSessionContext with Spring Boot 3 and Jetty server
[英]Error compilling Jetty embeded application : org.eclipse.jetty.server reads package jakarta.servlet from both jetty.servlet.api and jakarta.servlet
我正在構建一個帶有 Maven 的嵌入式 Jetty 應用程序。jetty.version:11.0.13 servlet-api.version:5.0.0。
這是一個多模塊項目。
一個名為“utils”的 maven 模塊依賴於 jakarta.servlet-api 進行編譯(范圍已提供)。 java 模塊需要 jakarta.servlet。
// 實用程序的 pom.xml
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
maven “應用程序”模塊添加了以下依賴項和其他。
// 應用程序的 pom.xml
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
另一個顯式依賴項之一取決於“utils”maven 模塊。
該應用程序的 Java 模塊包含:
requires utils;
requires org.eclipse.jetty.http;
requires org.eclipse.jetty.server;
requires org.eclipse.jetty.servlet;
當我編譯應用程序時,所有依賴模塊都編譯正常,但在編譯最后一個模塊(應用程序)時出現此錯誤:
myapplication.BlaBla.java:[46,17] 無法訪問 jakarta.servlet.http.HttpServlet class 找不到 jakarta.servlet.http.HttpServlet 文件
如果我添加以下依賴項:
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
並將以下行添加到我的應用程序模塊中:
requires jakarta.servlet;
我的編輯警告說:
模塊“application”從“jetty.servlet.api”和“jakarta.servlet”中讀取 package“jakarta.servlet”
並且構建顯然失敗並出現許多錯誤消息,例如:
[ERROR] module org.eclipse.jetty.server reads package jakarta.servlet from both jetty.servlet.api and jakarta.servlet [ERROR] module org.eclipse.jetty.server reads package jakarta.servlet.http from both jetty.servlet .api and jakarta.servlet [ERROR] module org.eclipse.jetty.server reads package jakarta.servlet.descriptor from both jetty.servlet.api and jakarta.servlet [ERROR] module org.eclipse.jetty.server reads package jakarta.來自 jetty.servlet.api 和 jakarta.servlet 的 servlet.annotation [...
如果我查看 jetty.servlet.api jar 文件,我看不到名為 jakarta.servlet 的 package。
我確實注意到“jetty-jakarta-servlet-api-5.0.2.jar”包含 jakarta.servlet 類,但我不知道在我的構建過程中使用它。
幫助?
似乎您正在嘗試讓 JPMS 與 Jetty 11 一起工作。
requires jakarta.servlet;
關於"module <blah> reads package <blah> from both <foo1> and <foo2>"
的錯誤告訴我這個。
這是您需要知道的。
來自 Maven Central 的 Jakarta Servlet 5.0.0的標准神器是...
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
</dependency>
這提供了(在 JPMS 術語中)名稱jakarta.servlet
查看該文件中的META-INF/MANIFEST.MF
,您會看到該行
Automatic-Module-Name: jakarta.servlet
由於...,這種依賴性(不幸的是)並不是為 JPMS 設計的。
module-info.class
(該文件不存在於官方依賴項中)jakarta.servlet
聲明為自動模塊名稱。 (基本上保留名稱以供以后升級到正確的module-info.class
)(順便說一句,即將推出的 Jakarta Servlet 6.0 已經解決了這個問題)。
那我為什么要告訴你這個?
Jetty 是 JPMS 的早期采用者,不得不解決 Jakarta Servlet 5.0 的一些缺陷。
一個是缺少的module-info.class
,另一個是缺少的模式(官方 Jakarta Servlet 5.0 中不存在)
運行 Jetty 服務器時jakarta.servlet
(JPMS 模塊名稱)版本 5.0 的替代品是......
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-jakarta-servlet-api</artifactId>
<version>5.0.2</version>
</dependency>
在實現 Jetty 服務器時使用它(就像您使用嵌入式 Jetty 一樣),並且根本不要使用官方依賴項。
Jetty Jakarta Servlet API 5.0.2 有一個module-info.class
聲明如下。
module jetty.servlet.api {
exports jakarta.servlet;
exports jakarta.servlet.annotation;
exports jakarta.servlet.descriptor;
exports jakarta.servlet.http;
exports jakarta.servlet.resources;
//open resources so Class.getResource() can access them
opens jakarta.servlet.resources;
}
將來,當您升級到 Jetty 12(目前處於 alpha 版本)時,您可以使用官方的jakarta.servlet
依賴項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.