Spring Boot RESTful Web Services Versioning Example

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.
Date: 2021-02-09
Building a Versioned RESTful API with Spring Boot
This article explores the creation of a RESTful Application Programming Interface (API) using Spring Boot, a popular Java framework for building standalone, production-grade Spring-based applications. Specifically, we will focus on implementing API versioning using the Accept Header method. Before diving into the specifics, let's establish a foundational understanding of the relevant technologies.
Spring Boot simplifies the process of building Spring applications. It provides a streamlined approach to configuration and setup, eliminating much of the boilerplate code typically associated with Spring development. Lombok, another tool often used in conjunction with Spring Boot, aids in reducing verbosity in Java code by automatically generating methods such as getters, setters, and constructors. Understanding these fundamental elements is beneficial, though a complete mastery isn't strictly required to follow this explanation.
The example uses common development tools: Eclipse as the Integrated Development Environment (IDE), Java Development Kit (JDK) version 8 for compilation and execution, and Maven as the build automation tool. Maven manages project dependencies, compiling the project, running tests, and packaging the application for deployment. The project structure adheres to standard Spring Boot conventions, making it easily navigable for those familiar with the framework.
Building the Application: A Step-by-Step Overview
The development process begins with defining the project's dependencies. A Project Object Model (POM) file, typically named pom.xml, lists all the necessary libraries. In this case, the POM file specifies dependencies for Spring Boot (including web and Java Persistence API (JPA) modules), Springdoc OpenAPI (for generating interactive API documentation using Swagger), an in-memory database such as H2 for development purposes, Java Faker for generating sample data, and Lombok for code simplification.
Next, a configuration file, often named application.yml, specifies application settings such as database connection details, server ports, and other configurable parameters. Developers are free to modify these settings to suit their specific environment and requirements.
The core of the application resides in several Java classes. One crucial class, often named VersioningRestfulServicesApplication, serves as the entry point of the application. This class is annotated with @SpringBootApplication, indicating its role as the main application class, and contains the main method responsible for launching the application.
Another critical component is a data transfer object (DTO) class, perhaps called EmployeeResponse, responsible for mapping data from the database to the API's response. This class strategically maps fields based on the media-type header received in the request, allowing for selective inclusion or exclusion of certain attributes in the response depending on the API version requested. This allows for flexibility and controlled evolution of the API's data structures.
The central piece of the application is the controller class, possibly named EmployeeController. This class handles incoming requests and generates appropriate responses. It uses HTTP GET mappings annotated with the Accept header to determine the requested API version. The Accept header contains the media type, such as application/vnd.jcg.app-1.0+json or application/vnd.jcg.app-2.0+json. Based on this header value, the controller selects the appropriate response, demonstrating API versioning based on the client's request.
Running and Testing the Application
Once the application is built using Maven, it can be executed directly from the IDE. After starting the application, testing can be performed using tools like Postman or the Swagger UI, which is automatically generated by Springdoc OpenAPI. The Swagger UI provides a user-friendly interface to interact with the API, allowing developers to test different endpoints and versions without manually crafting HTTP requests.
The API Endpoints
The application provides multiple endpoints to access employee data. Each endpoint is associated with a specific API version, identifiable through the Accept header in the HTTP request. The controller logic maps the header to the appropriate data structure and version of the employee data. This effectively manages different API versions without requiring separate URLs for each version, making it clean and efficient.
Conclusion
This article outlined the development of a versioned RESTful API using Spring Boot, leveraging the Accept header for versioning. This approach enables the management of multiple API versions simultaneously, facilitating backward compatibility and allowing for the gradual evolution of the API over time. By employing tools like Spring Boot, Lombok, and Maven, the development process is streamlined and efficient. The use of Swagger UI further improves the developer experience by providing a clear and interactive interface for testing the different API versions. This technique of versioning promotes maintainability and allows for a smooth transition when introducing new features or changing data structures. The use of DTOs helps tailor the response to the specific client's request, providing a cleaner and more efficient data exchange.