【问题标题】:HTTP Handler and Resteasy Deployment with undertow and resteasyHTTP Handler 和 Resteasy 部署与 undertow 和 resteasy
【发布时间】:2015-08-03 15:20:55
【问题描述】:

我正在尝试同时运行 HTTPServer 和 REST 处理程序。一次只能工作一个,不能同时工作。我需要提供 html 页面和 api。

这是我的代码。

    public class HttpServer {

    private final UndertowJaxrsServer server = new UndertowJaxrsServer();
    private static String rootPath = System.getProperty("user.dir");

    private final Undertow.Builder serverBuilder;

    public HttpServer(Integer port, String host) {
        serverBuilder = Undertow
                .builder()
                .addHttpListener(port, host)
                .setHandler(
                        Handlers.path().addPrefixPath(
                                "/",
                                Handlers.resource(
                                        new FileResourceManager(new File(
                                                rootPath + "/web"), 100))
                                        .addWelcomeFiles(
                                                rootPath + "/web/index.html")));
        server.start(serverBuilder);
    }

    public DeploymentInfo deployApplication(String appPath,
            Class<? extends Application> applicationClass) {
        ResteasyDeployment deployment = new ResteasyDeployment();
        deployment.setApplicationClass(applicationClass.getName());
        return server.undertowDeployment(deployment, appPath);
    }

    public void deploy(DeploymentInfo deploymentInfo) throws ServletException {
        server.deploy(deploymentInfo);
    }

    public static void main(String[] args) throws ServletException {
        HttpServer myServer = new HttpServer(8080, "0.0.0.0");

        DeploymentInfo di = myServer
                .deployApplication("/rest", MyApplication.class)
                .setClassLoader(HttpServer.class.getClassLoader())
                .setContextPath("/my").setDeploymentName("My Application");
        myServer.deploy(di);
    }
}

【问题讨论】:

  • 我面临同样的问题。你找到解决方法了吗?

标签: resteasy wildfly undertow


【解决方案1】:

从 3.1.0.Beta2 及更高版本您可以试试这个

UndertowJaxrsServer server = new UndertowJaxrsServer();

ResteasyDeployment deployment = new ResteasyDeployment();

deployment.setApplicationClass(ExampleApplication.class.getName());

DeploymentInfo deploymentInfo = server.undertowDeployment(deployment, "/");
deploymentInfo.setClassLoader(MyServer.class.getClassLoader());

deploymentInfo.setContextPath("/api");

server.deploy(deploymentInfo);

server.addResourcePrefixPath("/",
        resource(new PathResourceManager(Paths.get(STATIC_PATH),100)).
            addWelcomeFiles("index.html"));

server.start();

RestEasy 应用程序将在 /api/* 和 /* 中提供静态文件

【讨论】:

    【解决方案2】:

    UndertowJaxrsServer 正在覆盖构建器的文件处理程序:

    public UndertowJaxrsServer start(Undertow.Builder builder)
    {
        server = builder.setHandler(root).build();
        server.start();
        return this;
    }
    

    似乎没有办法将另一个处理程序传递给 UndertowJaxrsServer。可能的解决方法可能是:

    • 使用一个资源类部署另一个应用程序,该资源类仅提供文件服务。
    • 使用空白的 Undertow 并放松轻松的 JAX-RS 部署。

    【讨论】:

    • 嗨,我也面临同样的问题。我怎么能“使用空白的 Undertow 并放松轻松的 JAX-RS 部署”?
    • 对于第一个解决方法“使用一个资源类部署另一个应用程序,它只提供文件。”,这将如何工作?如果运行两个应用程序,它们会监听不同的端口号,对吧?
    猜你喜欢
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2018-01-01
    • 2018-10-25
    相关资源
    最近更新 更多