I came up with this algorithm when building a REST API server. I wanted to be able to generate methods declared as endpoints in the code on the fly. The original ideas we had was either declare the list of endpoints in a database, properties/xml file, or have the code dynamically generate the endpoints when needed.
Since most the Objects in the original code was proprietary, the closest parallel library I can use as an example was the one that inspired it: Spring Boot.
Most seasoned Spring Developers would probably point out the usage of Spring Actuator or whatnot but note that we're not actually using this in my original code. The algorithm's aim was to be agnostic so rather than use Spring's built-in APIs I'm just merely using its Annotations as practical examples of how to use the algorithm.
The algorithm is as follows:
- Get
this
class or any target class' declared methods. - Check if that method is declared as
RequestMapping
. This tells us that this method is an endpoint. - If an explicit value is set in the annotation use that as the name of the endpoint, otherwise, assume the method name is endpoint's name.
- Here's the tricky part. Because java cannot tell you the paramter's name at run time - it iterates over tham as
argN
- we'll need to use some other indicator to get the name. In this case, Spring has aRequestParam(name="")
annotation. We used a similar annotation approach in our company's code so we were able to extract a more meaningful string label out of it. You may or may not need this but having a label helps developers identify what the parameters are for. - Get the list of supported request methods.
- Finally populate an array of all those endpoints and return it.
Just like in Spring Boot, our framework also returns the results in JSON format.