How to get all exposed routes in Spark framework

934 Views Asked by At

In java spark web framework a route is defined as

get("/", (request, response) -> {
// Show something

});

In my application I define multiple routes. Is there any way I can get all the exposed routes of the application?

2

There are 2 best solutions below

1
On

It's possible now in spark 2.9.4

import spark.Spark;
import spark.routematch.RouteMatch;

// somewhere after route definitions

for (RouteMatch route : routes()) {
    String method = route.getHttpMethod().toString();
    if (method.equals("before") || method.equals("after")) continue;
    System.out.printf("--> %-7s %s\n", method.toUpperCase(), route.getMatchUri());
}

output

--> POST    /internal/update_notification
--> GET     /internal/history
--> GET     /internal/history_details
--> GET     /ping
0
On

As far as I know, there's no way to automatically find REST routes as they do not have a standard registry service. Although, you can document them using Swagger or even store them using chrome applications such as Postman or Advanced Rest Client.