Kev*_*man 7 servlets jetty maven jakarta-migration
这是这个问题的后续。我不认为这是重复的,因为接受的答案表明 Jetty 11 不适用于javaxservlet,但我问为什么 Jetty 11 不适用于jakartaservlet。
我这里有一个示例项目,它使用 Jetty 9 部署本地服务器,包括javax使用@WebServlet注释的 servlet。这一切都很好。
现在我尝试更新它以使用 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>
Run Code Online (Sandbox Code Playgroud)
服务器主程序.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();
}
}
Run Code Online (Sandbox Code Playgroud)
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>");
}
}
Run Code Online (Sandbox Code Playgroud)
索引.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>
Run Code Online (Sandbox Code Playgroud)
我可以使用运行服务器mvn package exec:java并且它启动得很好。
我的index.html文件加载正确:

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

我在命令行中没有看到任何错误(或根本没有任何输出)。
我还应该提到,我的 Jakarta servlet 可以与 Jetty 11 的完整版本配合良好(使用 Jetty 主目录和 Jetty 基本目录,如此处所述)。只是这个“独立”Jetty 服务器启动器似乎有问题,所以我对我的ServerMain.java文件感到怀疑。
我需要更改什么才能让 Jetty 11 检测我的 Jakarta servlet?
删除配置工作,它使用错误,并且是问题的根源。
这些行,删除它们。
// Enable annotations so the server sees classes annotated with @WebServlet.
webAppContext.setConfigurations(new Configuration[]{
new AnnotationConfiguration(),
new WebInfConfiguration(),
});
Run Code Online (Sandbox Code Playgroud)
从 Jetty 10 开始,您不再以Configuration这种方式管理选项。类路径中存在具有该功能的 jar 就足够了。(在你的情况下是jetty-annotations-<ver>.jar)
其次,您永远不会使用setConfigurations()那么小的列表Configuration(您遗漏了许多必需的Configuration条目)。
addConfiguration(Configuration...)相反,您应该只使用该方法new AnnotationConfiguration(),但前提是该特定方法Configuration不可自动发现(可以自动发现)jetty-annotation。
server.setDumpAfterStart(true)提示,在调用之前使用server.start(),并查看服务器状态是什么,包括您的WebAppContext外观及其Configuration条目。这是查看您的更改对服务器和上下文造成的影响的好方法。
以下是 Jetty 11 的示例项目,使用@WebServlet、 和WebAppContext。
| 归档时间: |
|
| 查看次数: |
4346 次 |
| 最近记录: |