问题
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