One of the best ways to write a RESTFul api application is to add version control. eg:
http://my-server/api/v1/getData http://my-server/api/v2/getData
Our application provides REST api using the Spring framework. We mark the class as a controller, use the RequestMapping annotation to map the URL of the function, and add some objects that were translated to / from json objects.
For instance:
@RequestMapping(method = RequestMethod.POST, value = "/api/v1/getData") public @ResponseBody ResponseDataDTO getData(@RequestBody OperationsDetailsTDO details) {...}
Now we want to provide the second version of the API. About 2/3 of the functions remain unchanged, and 1/3 change. Changes occur in both logical and JSON objects.
I wonder how to create code. I find this kind of code difficult to handle:
@RequestMapping(method = RequestMethod.POST, value = "/api/{version-var}/getData") public @ResponseBody ResponseDataDTO createReleaseFromTemplate(@PathVariable("version-var") Integer version, @RequestBody OperationsDetailsTDO details) { if (version == 1) { doForVersion1(); } else if (version == 2) { doForVersion2(); } }
This will be difficult, since each function will have a different branch. To demonstrate the problem, if I have an automatic tool that generates documentation, it will not be able to understand what the API is.
Secondly, I am wondering what should I do with classes bound to a JSON object. Do all these classes need to be duplicated for minor changes?
thanks.
source share