DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]
  • How to Make a REST API Call in Angular
  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)

Trending

  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Infrastructure as Code (IaC) Beyond the Basics
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. REST API Versioning Strategies

REST API Versioning Strategies

Today microservices are a hot trend for developing cloud-native applications. API versioning helps to iterate faster when the needed changes are identified.

By 
Praveen KG user avatar
Praveen KG
·
Sep. 23, 21 · Analysis
Likes (12)
Comment
Save
Tweet
Share
11.7K Views

Join the DZone community and get the full member experience.

Join For Free

Today microservices are a hot trend for developing cloud-native applications with hosts of benefits including, improved scalability, resilient applications, technology-agnostic,  faster time to market, greater business agility with CICD support.

One of the key building blocks of microservice-based architecture is the API design and contract. With API, it is important to have versioning that enables clients to use the existing REST API continuously and migrate their applications to the new API whenever they are ready. 

When to Version?

Versioning helps us to navigate the breaking changes in API like changes made to the request format by introducing mandatory parameters, changes made to the response data either due to format change or structure of the response message, or deprecation of the API to provide the enhanced functionalities.

What Are the Different Ways We Can Versionize the API?

There are four different ways to version the REST API.

  • Versioning through URI Path
  • Versioning through Query Parameters
  • Versioning through Custom Headers
  • Versioning through content negotiations

Version Through URI path
It is one of the most common and straightforward ways to version the endpoints. Version need not be always numeric, or in v[x] format, you can use any meaningful identifier like data or release numbers, which helps API producing team to incorporate new changes seamlessly.

http://api.example.com/v1

Versioning Through Query Parameters

Another option is to use the version as a query parameter like:

http://api.example.com/customers?version=v1

This approach is simple and easy to implement,  we can make the latest API version as the default unless the version is specified explicitly.

 Versioning Through Custom Headers

We can also implement the versioning through custom headers. This helps to avoid any fillers to the URI.

Java
 
@RestController

@RequestMapping("/")

public class ProductController {
	
  	@Autowired
     private ProductRepository repository;

  	@GetMapping(value= "products", headers = {"X-API-VERSION=v1"})
    public List<Product> findAll() {
           return repository.findAll();
     }

     @GetMapping(value= "products", headers = {"X-API-VERSION=v1"})
     public List<Product> findAllV2() {
           return repository.findAll();
     }
}


The only downside of this approach is that it requires maintaining a header to use the version and processing of headers.

Versioning Through Content negotiations

This approach helps clients to specifically request a version with Accept Header. Service can be implemented to deliver the default representation If clients don't request a specific version.

GET /customers/1234 HTTP/1.1 Accept: application/vnd.v1+json

Java
 
@RestController
@RequestMapping("/")
public class ProductController {


	 @Autowired
	 private ProductRepository repository;

	    // Find
	    @GetMapping(value= "products", headers = {"Accept=application/vnd.v1+json"})
	    List<Product> findAll() {
	        return repository.findAll();
	    }

	    @GetMapping(value= "products", headers = {"Accept=application/vnd.v2+json"})
	    List<Product> findAllV2() {
	        return repository.findAll();
	    }
	   
}


With this,  we have  enabled  2 versions of the GET /productsendpoint with the Accept header. When the curl request is made with v1 of the header value, the response would be according to version v1.

curl -L -X GET 'http://localhost:8080/products' \ -H 'Accept: application/vnd.v1+json'
 [    
    {       
        "name": "IdeaPad Slim 5 (15, AMD)"  
    } 
]


When the curl request is made with v2 of the header value, the response would be according to version v2.

JSON
 
curl -L -X GET 'http://localhost:8080/products' \
-H 'Accept: application/vnd.v2+json'

[
    {
        "name": "IdeaPad Slim 5 (15, AMD)"
     }
]


When no Accept the header is sent, it would respond with the default version, ie v1 here:

JSON
 
curl -L -X GET 'http://localhost:8080/products' 
 [    
    {       
        "name": "IdeaPad Slim 5 (15, AMD)"  
    } 
]


406 Not Acceptable


406 Not Acceptable

JSON
 
curl -L -X GET 'http://localhost:8080/products' 
-H 'Accept: application/vnd.v3+json'

{
    "timestamp": "2021-09-13T14:30:12.263+0000",
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/products"
}


Note: GitHub has implemented versioning with content negotiation take a look at that in their documentation.

Summary

As API-driven architectures are evolving and more and more people are adopting, it is important to version the APIs to minimize the disruption with changes to API and to have better backward compatibility.  Each of the above-mentioned versioning techniques helps with its own pros and cons. 

API REST Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]
  • How to Make a REST API Call in Angular
  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!