Serving static content in UndertowJaxrsServer

*爱你&永不变心* 提交于 2019-12-10 11:58:41

问题


I'm trying to serve static content through a ResourceHandler in my Undertow server that has a RestEasy deployment.

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

With the above code, only the resteasy deployment works and I get a 404 for the static content (index.html).

Any pointers? Thanks!


回答1:


The UndertowJaxrsServer API is a bit tricky. Although you can configure an Undertow.Builder to start the server, the associated handler is replaced by a default PathHandler instance, which is also used to configure the REST application.

So, the proper way of adding more HttpHandlers (such as a ResourceHandler), is by using the UndertowJaxrsServer#addResourcePrefixPath method, to specify extra handlers for your requests.

Here is an example of utilizing the above API to successfully serve static content in addition to REST resources: https://gist.github.com/sermojohn/928ee5f170cd74f0391a348b4a84fba0




回答2:


This is from one of my toy projects:

public static void main(String[] args) {
    UndertowJaxrsServer server = new UndertowJaxrsServer();

    ResteasyDeployment deployment = new ResteasyDeployment();
    deployment.setApplicationClass(RestEasyConfig.class.getName());
    deployment.setInjectorFactoryClass(CdiInjectorFactory.class.getName());

    DeploymentInfo deploymentInfo = server.undertowDeployment(deployment)
            .setClassLoader(GatewayApi.class.getClassLoader())
            .setContextPath("/api")
            .addFilter(new FilterInfo("TokenFilter", TokenFilter.class))
            .addFilterUrlMapping("TokenFilter", "/*", DispatcherType.REQUEST)
            .addFilterUrlMapping("TokenFilter", "/*", DispatcherType.FORWARD)
            .addListener(Servlets.listener(Listener.class))
            .setDeploymentName("Undertow RestEasy Weld");

    server.deploy(deploymentInfo);
    server.addResourcePrefixPath("/index.htm", resource(new ClassPathResourceManager(GatewayApi.class.getClassLoader()))
                    .addWelcomeFiles("webapp/index.htm"));

    Undertow.Builder undertowBuilder = Undertow.builder()
            .addHttpListener(8080, "0.0.0.0");
    server.start(undertowBuilder);
    log.info(generateLogo());
}


来源:https://stackoverflow.com/questions/45243452/serving-static-content-in-undertowjaxrsserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!