【问题标题】:Serving static content in UndertowJaxrsServer在 UndertowJaxrsServer 中提供静态内容
【发布时间】:2017-12-27 20:20:13
【问题描述】:

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

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)。

任何指针?谢谢!

【问题讨论】:

    标签: resteasy undertow undertowjaxrsserver


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这是我的一个玩具项目:

      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());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 2011-04-18
        • 2018-01-29
        相关资源
        最近更新 更多