Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Handler and Resteasy Deployment with undertow and resteasy

I am trying to run both HTTPServer and also the REST Handler. Only one works at a time not able to make it work both at same time. I need to serve html pages and also the api.

here is my code.

    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);
    }
}
like image 614
Ramesh Avatar asked May 21 '15 19:05

Ramesh


1 Answers

The UndertowJaxrsServer is overriding the filehandler of your builder:

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

Seems like there is no way to pass another handler to the UndertowJaxrsServer. Possible workarounds might be:

  • Deploy another application with one resource class which simply serves files.
  • Use the blank Undertow and loose the comfort of easy JAX-RS deployments.
like image 122
lefloh Avatar answered Oct 20 '22 00:10

lefloh