4

I'm creating a REST API using Spring Boot, and I thought to return a list (as JSON) of all available routes when the user makes a request to, say, /help or even simply to the root endpoint of the server, i.e., in my case since I'm working locally, localhost:8080.

I know I can see the available routes in the logs when, e.g., the Spring Boot application starts, but I'm not sure how can I access these routes from a Spring Boot controller. As I said, these routes would be returned as JSON, something like:

{

    routes: [
        "/api/users/create",
        "/api/users/list",
        ...
    ]

}

of course, it would also be nice to provide additional required information to make the requests to the specific URLs, e.g. if the client needs to pass certain request parameters, which ones, and in which format.

For example, it would be something of the form:

{

    "routes" : [
        {"route": /api/users/create", "requestParams": ["name", "age", ... ]},
        ...
    ]

}

Yes, I thought, with this method, to provide some kind of documentation to a client trying to use the REST services which I created.

How can I do this? Is there a simple way at least of accessing the routes?

Would there be any problems in doing this? If yes, which ones?

nbro
  • 15,395
  • 32
  • 113
  • 196
  • Spring boot actuator does its out-of-box for you via `/mappings` endpoint. check [here](http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html) for more details – Bond - Java Bond Feb 28 '17 at 12:47
  • @Bond-JavaBond I wanted also to mention Spring Boot actuator (since I'm already using it), which actually, from what I understood, should be used actually while developing; for example, to test the status of the application. If this is true, then it was not thought to be used for a case similar to what I'm describing, but I could be wrong, since I do not know much about actuator. – nbro Feb 28 '17 at 12:53
  • Actuator isn't only for development. There's no reason why you can't use it in production. – Daniel Olszewski Feb 28 '17 at 13:00
  • @DanielOlszewski Anyway, `/mappings` does not provide all the info that I would like to show, like required parameters. Furthermore, it shows irrelevant info. It would eventually be nice to access these JSONs from a controller and modify them according to our needs. Is it possible? – nbro Feb 28 '17 at 13:21

2 Answers2

17

You can use RequestMappingHandlerMapping

Inject it:

@Autowired
public RequestMappingHandlerMapping requestMappingHandlerMapping;

Then:

@RequestMapping("/endpoints")
public @ResponseBody
Object showEndpointsAction() throws SQLException
{
        return requestMappingHandlerMapping.getHandlerMethods().keySet().stream().map(t ->
               (t.getMethodsCondition().getMethods().size() == 0 ? "GET" : t.getMethodsCondition().getMethods().toArray()[0]) + " " +                    
               t.getPatternsCondition().getPatterns().toArray()[0]
        ).toArray();
 }

Should list all endpoints URL path.

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
2

Do you consider the possibility to use Swagger for give a documentation for you API? is simple to use, with some annotations you will have a complete documentation for you API. Here you have a guide how to configure Swagger. I hope that it help you.

Marcos Echagüe
  • 547
  • 2
  • 8
  • 21