List all registered routes in Vertx

情到浓时终转凉″ 提交于 2019-12-10 21:26:01

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/38463145/list-all-registered-routes-in-vertx

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