2015-05-21 1 views
6

Я пытаюсь запустить HTTPServer, а также обработчик REST. Только один работает одновременно, не в состоянии заставить его работать одновременно. Мне нужно обслуживать html-страницы, а также api.Развертывание HTTP-дескриптора и рестайлинга с подбором и отдыхом

вот мой код.

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

The UndertowJaxrsServer является переопределение filehandler вашего строителя:

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 на/*

Смежные вопросы