2017-07-21 61 views
0

我試圖在具有RestEasy部署的Undertow服務器中通過ResourceHandler提供靜態內容。在UndertowJaxrsServer中爲靜態內容提供服務

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部署工作,我得到了靜態內容(index.html)的404。

任何指針?謝謝!

回答

0

UndertowJaxrsServer API有點棘手。雖然您可以配置Undertow.Builder來啓動服務器,但關聯的處理程序將被替換爲默認的PathHandler實例,該實例也用於配置REST應用程序。

因此,添加更多HttpHandler(例如ResourceHandler)的正確方法是使用UndertowJaxrsServer#addResourcePrefixPath方法爲請求指定額外的處理程序。

以下是利用上述API成功提供除REST資源外的靜態內容的示例:https://gist.github.com/sermojohn/928ee5f170cd74f0391a348b4a84fba0