Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all registered routes in Vertx

Is there any way (like in a symfony application) to list down all the available/register routes on a vertx server? I am facing an issue that my registered route is returning a 404 on running restassure tests.

like image 677
Obaid Maroof Avatar asked Feb 07 '23 13:02

Obaid Maroof


1 Answers

Yes, but you have to write a bit of code to achieve that.

// Example router setup
Router router = Router.router(vertx);
router.route(HttpMethod.GET, "/").handler(routingContext -> {
    routingContext.response().end("Root");
});

router.route(HttpMethod.GET, "/users").handler(routingContext -> {
    routingContext.response().end("Post");
});

router.route(HttpMethod.POST, "/users").handler(routingContext -> {
    routingContext.response().end("Post");
});

// Getting the routes
for (Route r : router.getRoutes()) {
    // Path is public, but methods are not. We change that
    Field f = r.getClass().getDeclaredField("methods");
    f.setAccessible(true);
    Set<HttpMethod> methods = (Set<HttpMethod>) f.get(r);
    System.out.println(methods.toString() + r.getPath());
}

This will result in:

[GET]/
[GET]/users
[POST]/users
like image 168
Alexey Soshin Avatar answered Feb 20 '23 02:02

Alexey Soshin