Versioning through content-negotiation

Providing the version information through the Accept (request) header along with the content-type (media) in response is the preferred way as this helps to version APIs without any impact on the URI. As we've already learned about content-negotiation, in Chapter 2, Design Strategy, Guidelines, and Best Practices, let's jump to a code implementation of versioning through Accept and Content-Type:

@GetMapping(value = "/investorsbyacceptheader",headers = "Accept=application/investors-v1+json,application/investors-v1.1+json")public List<Investor> fetchAllInvestorsForGiven..()throws VersionNotSupportedException {return getResultsAccordingToVersion("1.1");}

The following screenshot is the output of the preceding ...

Get Hands-On RESTful API Design Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.