The vertx-web documentation says that it is possible to serve static resources from the classpath, and everything I've read in forum/help threads seems to confirm this. Unfortunately I can't seem to get it to work.
public class MainVerticle extends AbstractVerticle {
@Override
public void start() {
final Router router = Router.router(vertx);
router.get("/greeting").handler(req -> req.response().end("Hello Vert.x!"));
router.route("/*").handler(StaticHandler.create());
vertx.createHttpServer()
.requestHandler(router)
.listen(8080);
}
}
@ExtendWith(VertxExtension.class)
class MainVerticleTest {
@BeforeEach
void prepare(Vertx vertx, VertxTestContext testContext) {
vertx.deployVerticle(MainVerticle.class.getCanonicalName(), testContext.succeeding(id -> testContext.completeNow()));
}
@Test
@DisplayName("Check that the server returns greeting")
void checkServerHasStarted(Vertx vertx, VertxTestContext testContext) {
WebClient webClient = WebClient.create(vertx);
webClient.get(8080, "localhost", "/greeting")
.as(BodyCodec.string())
.send(testContext.succeeding(response -> testContext.verify(() -> {
assertEquals(200, response.statusCode());
assertTrue(response.body().length() > 0);
assertTrue(response.body().contains("Hello Vert.x!"));
testContext.completeNow();
})));
}
@Test
@DisplayName("Check that the server returns router-root.txt")
void checkServerHasRootResource(Vertx vertx, VertxTestContext testContext) {
WebClient webClient = WebClient.create(vertx);
webClient.get(8080, "localhost", "/router-root.txt")
.as(BodyCodec.string())
.send(testContext.succeeding(response -> testContext.verify(() -> {
assertEquals(200, response.statusCode());
testContext.completeNow();
})));
}
@Test
@DisplayName("Check that the server returns router-package.txt")
void checkServerHasPackageResource(Vertx vertx, VertxTestContext testContext) {
WebClient webClient = WebClient.create(vertx);
webClient.get(8080, "localhost", "/router-package.txt")
.as(BodyCodec.string())
.send(testContext.succeeding(response -> testContext.verify(() -> {
assertEquals(200, response.statusCode());
testContext.completeNow();
})));
}
}
checkServerHasStarted
passes, but the other two are failing with 404
. The two resource files exist here:
src/main/resources/router-root.txt
src/main/resources/io/vertx/starter/router-package.txt
And I have confirmed these files exist in the fat jar at their relative paths.
Ideally I would like to include these resources from another bundle (java project), but first things first.
Is there something I need to do with my routes to get either one of the failed tests to pass? I am just getting started with Vert.x, so I assume my setup is stupid somewhere.
Full Project: https://github.com/drkstr101/vertx-static-example
Cheers, and thanks in advance for any guidance!
EDIT
I realize there is a form of StaticHandler.create
that accepts a ClassLoader
, and is most likely how to go about this. However from what I read this should not be necessary, and also there is a deprecating warning about the feature being removed in the next version. May I assume that that serving resources from the classpath will be fully working once the feature is deprecated?
The default folder for resources is
webroot
.Create a
src/main/resources/webroot
folder in your project and move all the static content to it. Or configure theStaticHandler
to change the root folder.Side note: for a catch-all route you can omit the path