REST API Versionin
What is Semantic Versioning 2.0.0 ?
Given a version number MAJOR.MINOR.PATCH, increment the:
Why do we need API versioning ?
One of the major challent5ges surrounding exposing services is handling updates to the API contract :
What are the possible the breaking changes ?
What are Effective change management principles ?
4 REST API Versioning Strategies :
This solution often uses URI routing to point to a specific version of the API. you may consider API gateways.When a new version of the REST API is released, it is perceived as a new entry in the cache.
Note : The major and minor version changes can be a part of the URI
@GetMapping({"/v1/investors","/v1.1/investors","/v2/investors"})
public List<Investor> fetchAllInvestors()
{
return investorService.fetchAllInvestors();
}
2. Versioning through query parameters : http://hostname/investors?version=2.0
Recommended by LinkedIn
@GetMapping("/investors")
public List<Investor> fetchAllInvestorsForGivenVersionAsParameter(
@RequestParam("version") String version)
throws VersionNotSupportedException {
if (!(version.equals("1.1") || version.equals("1.0"))) {
throw new VersionNotSupportedException("version " + version);
}
return investorService.fetchAllInvestors();
})
3. Versioning through custom headers
curl -H “x-source-version: 2.0” http://hostname/investors
@GetMapping("/investorsbycustomheaderversion"
public List<Investor> fetchAllInvestors...(
@RequestHeader("x-resource-version") String version)
throws VersionNotSupportedException {
return getResultsAccordingToVersion(version);
})
4. Versioning through content negotiation
curl -H “Accept: application/investors-v1.1+json” http://hostname/investors
@GetMapping(value = "/investorsbyacceptheader"
headers = "Accept=application/investors-v1+json,
application/investors-v1.1+json")
public List<Investor> fetchAllInvestorsForGiven..()
throws VersionNotSupportedException {
return getResultsAccordingToVersion("1.1");
}
Then response header content-type= Accept=application/investors-v1+json
PLEASE NOTE : You can use the URL path versioning with the major and include the complete version in the request using custom headers.
Refrences :
[1] https://www.xmatters.com/blog/blog-four-rest-api-versioning-strategies/
[2] https://semver.org/
[3] O'rielly : Hands-On RESTful API Design Patterns and Best Practices
[4] https://www.freecodecamp.org/news/how-to-version-a-rest-api/