有没有办法将 Servlet API 与 Undertow 一起使用?

Is there a way to use the Servlet API with Undertow?

我刚刚发现 Undertow 的工作原理,我对其感到惊讶 api:

Undertow server = Undertow.builder()
                .addHttpListener(8080, "localhost")
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServerExchange exchange) throws Exception {
                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                        exchange.getResponseSender().send("Hello World");
                    }
                }).build();
        server.start();

有没有办法像这样使用更方便的servlet api

Undertow server = Undertow.builder()
                .addHttpListener(8080, "localhost")
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
                        // ...
                    }
                }).build();
        server.start();

我想要实现的是用 Undertow 替换当前工作的 Jetty 容器,它使用 servlet api 但在阅读文档和源代码后我似乎无法找到这样做的方法。我没有使用 .war 文件,只是一个嵌入式 Jetty。有人有任何指示吗?

它与 Servlet API.

一起记录在 Creating a Servlet Deployment. Here's a MCVE based on the documentation provided that you've the dependencies 部分的右侧
package com.Whosebug.q35269763;

import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;

import java.io.IOException;

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

public class Test {

    public static void main(String... args) throws Exception {
        DeploymentInfo servletBuilder = Servlets.deployment().setClassLoader(Test.class.getClassLoader())
            .setDeploymentName("myapp").setContextPath("/myapp")
            .addServlets(Servlets.servlet("myservlet",
                new HttpServlet() {
                    @Override
                    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                        response.getWriter().write("Hello World!");
                    }
                }.getClass()).addMapping("/myservlet"));
        DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
        manager.deploy();
        PathHandler path = Handlers.path(Handlers.redirect("/myapp")).addPrefixPath("/myapp", manager.start());
        Undertow server = Undertow.builder().addHttpListener(8888, "localhost").setHandler(path).build();
        server.start();
    }

}

当您在复制'n'粘贴'n'运行以上代码后在您最喜欢的网络浏览器中打开http://localhost:8888/myapp/myservlet时,您会看到

Hello World!