Static Handler not working for Webroot in Fat Jar

454 views
Skip to first unread message

Lin

unread,
Sep 30, 2016, 2:08:48 AM9/30/16
to vert.x
Hi:

I am building a fat jar for a Vertx-Web application. I would like to serve some static files. I packaged the jar file, with webroot folder. See below screenshot for my jar structure:

enter image description here

I was able to load the webroot/static/test.html file by doing:

routingContext.response().sendFile("webroot/static/test.html");

However, I am not able to get the static handler to work. Below is my full code:

package com.jdescript;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;

public class WebVerticle extends AbstractVerticle {
    private HttpServer httpServer;

    @Override
    public void start() throws IOException {
        httpServer = vertx.createHttpServer();

        Router router = Router.router(vertx);
        router.route("/static/*").handler(StaticHandler.create());

        router.route("/test").handler(routingContext -> {
            routingContext.response().sendFile("webroot/static/test.html");
        });

        httpServer.requestHandler(router::accept).listen(9999);
    }
}

In the above example, http://localhost:9999/static/test.html will say "Not Found", whilehttp://localhost:9999/test will render test.html.

Any help will be appreciated.


Julien Viet

unread,
Sep 30, 2016, 3:37:58 AM9/30/16
to ve...@googlegroups.com
Hi,

it seems related to naming and conventions: you need an index.html in webroot (and not in webroot/static)

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/c0710d0d-7ee6-4f0a-8cb8-d79e3afb1c08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lin

unread,
Sep 30, 2016, 3:44:04 AM9/30/16
to vert.x
Ha, that was it! Thank you very much for the help!
Reply all
Reply to author
Forward
0 new messages