Manage HTTP handler and restyling using and rest

I am trying to run HTTPServer as well as a REST handler. Only one works at a time, unable to make it work at the same time. I need to serve html pages as well as 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); } } 
+6
source share
2 answers

UndertowJaxrsServer overrides your builder's file handler:

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

It seems that it is not possible to pass another handler to the UndertowJaxrsServer. Possible workarounds could be:

  • Deploy another application with one resource class that just serves the files.
  • Use an empty Undertow and ease the ease of deployment of the JAX-RS.
+4
source

From version 3.1.0.Beta2 and higher you can try this

 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 will be available in / api / * and static files in / *

0
source

Source: https://habr.com/ru/post/987690/


All Articles