0

I have a rest api and I want to manage a list of the URLs offered by my rest api. I would rather not do this manually. Does anyone know a way to generate the list of URLs programmatically by somehow scanning the rest api? I have been researching this for a while and have come up with nothing.

Will Harrison
  • 565
  • 3
  • 8
  • 21

1 Answers1

0

I see two solutions to do that:

  • The static one. I mean implementing a small Java application that scans the classes of your REST application to detect the ones with annotation @Controller. You can then introspect the class to get hints regarding the provided endpoints. See a sample of controller below:

    @RestController
    public class GreetingController {
        @RequestMapping("/greeting")
        public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
            return new Greeting(counter.incrementAndGet(),
                        String.format(template, name));
        }
    }
    

    The annotation @RequestMapping give you the resource path (path variables are contained inside if any), the @RequestParam one the parameter.

    Based on this, we can then generate some Swagger or RAML content (or something else).

    For information, Spring provides a convenient way to scan the classpath to easily get all classes that have a specific annotation. See this answer for more details: Scanning Java annotations at runtime.

  • The other one could be during execution. I don't know if Spring holds something about REST routes and so on like the Restlet framework does. See this link https://restlet.com/technical-resources/restlet-framework/guide/2.3/extensions/apispark/introspector.

Hope it helps. Thierry

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360