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.
-
You need to provide way more information than this. What API framework are you using? ASP.net Web API? ServiceStack? Django? Depending on what you're using, there are ways to do this... – user1431072 Jan 28 '15 at 22:50
-
@user1431072 I am using the Spring Framework – Will Harrison Jan 28 '15 at 23:10
1 Answers
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

- 1
- 1

- 198,364
- 44
- 396
- 360