2015-05-21 34 views
6

我想運行HTTPServer和REST處理程序。一次只能工作,無法同時工作。我需要提供html頁面和api。HTTP處理程序和resteasy部署與高級和resteasy

這是我的代碼。

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); 
    } 
} 
+0

我面臨着同樣的文件問題。你有沒有想出一個解決方法? – yyff

回答

4

UndertowJaxrsServer是壓倒你的建設者的文件處理器:

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

好像沒有辦法到另一個處理程序傳遞給UndertowJaxrsServer。可能的解決方法可能是:

  • 使用一個資源類部署另一個應用程序,該資源類僅提供文件。
  • 使用空白的Undertow並放鬆輕鬆JAX-RS部署的舒適度。
+0

嗨,我面臨同樣的問題。我怎樣才能「使用空白的Undertow並放鬆簡單的JAX-RS部署的舒適性」? – yyff

+0

第一種解決方法「使用一個只提供文件的資源類來部署另一個應用程序」,這將如何工作?如果運行兩個應用程序,他們將聽不同的端口號,對吧? – yyff

0

從版本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/*和static/*