0

我正在尝试通过ResourceHandler具有 RestEasy 部署的 Undertow 服务器中的 a 提供静态内容。

public class Server {
public static void main(String[] args) throws Exception {
    UndertowJaxrsServer server = new UndertowJaxrsServer();
    Undertow.Builder serverBuilder = Undertow
            .builder()
            .addHttpListener(8080, "0.0.0.0")
            .setHandler(
                    Handlers.path().addPrefixPath(
                            "/web",
            new ResourceHandler(new PathResourceManager(Paths.get("/some/fixed/path"),100))
                    .setDirectoryListingEnabled(true)
                    .addWelcomeFiles("index.html")));

    ResteasyDeployment deployment = new ResteasyDeployment();
    deployment.setApplicationClass(MyRestApplication.class.getName());
    DeploymentInfo deploymentInfo = server.undertowDeployment(deployment, "/")
            .setClassLoader(Server.class.getClassLoader())
            .setContextPath("/api").setDeploymentName("WS");
    server.deploy(deploymentInfo);
    server.start(serverBuilder); 
  }
}

使用上面的代码,只有 resteasy 部署工作,我得到一个 404 的静态内容(index.html)。

任何指针?谢谢!

4

2 回答 2

1

这是来自我的一个玩具项目:

public static void main(String[] args) {
    UndertowJaxrsServer server = new UndertowJaxrsServer();

    ResteasyDeployment deployment = new ResteasyDeployment();
    deployment.setApplicationClass(RestEasyConfig.class.getName());
    deployment.setInjectorFactoryClass(CdiInjectorFactory.class.getName());

    DeploymentInfo deploymentInfo = server.undertowDeployment(deployment)
            .setClassLoader(GatewayApi.class.getClassLoader())
            .setContextPath("/api")
            .addFilter(new FilterInfo("TokenFilter", TokenFilter.class))
            .addFilterUrlMapping("TokenFilter", "/*", DispatcherType.REQUEST)
            .addFilterUrlMapping("TokenFilter", "/*", DispatcherType.FORWARD)
            .addListener(Servlets.listener(Listener.class))
            .setDeploymentName("Undertow RestEasy Weld");

    server.deploy(deploymentInfo);
    server.addResourcePrefixPath("/index.htm", resource(new ClassPathResourceManager(GatewayApi.class.getClassLoader()))
                    .addWelcomeFiles("webapp/index.htm"));

    Undertow.Builder undertowBuilder = Undertow.builder()
            .addHttpListener(8080, "0.0.0.0");
    server.start(undertowBuilder);
    log.info(generateLogo());
}
于 2018-07-31T01:04:12.960 回答
1

UndertowJaxrsServer API 有点棘手。尽管您可以配置一个 Undertow.Builder 来启动服务器,但关联的处理程序被默认的 PathHandler 实例替换,该实例也用于配置 REST 应用程序。

因此,添加更多 HttpHandler(例如 ResourceHandler)的正确方法是使用 UndertowJaxrsServer#addResourcePrefixPath 方法为您的请求指定额外的处理程序。

以下是利用上述 API 成功提供静态内容以及 REST 资源的示例:https ://gist.github.com/sermojohn/928ee5f170cd74f0391a348b4a84fba0

于 2017-11-02T22:30:04.993 回答