Mastering API Versioning in Spring Boot 4.0 with Spring Framework 7
This article explains how Spring Framework 7, bundled in Spring Boot 4.0 snapshot, introduces native API version control via a new `version` attribute on `@RequestMapping`, demonstrates basic and advanced configuration examples, showcases common versioning strategies, and provides testing commands to help developers manage API evolution efficiently.
Basic Usage: RequestMapping version attribute
Spring Framework 7.0 adds a
versionattribute to
@RequestMappingand its derived annotations, making version control concise.
<code>@RequestMapping
@RestController
public class DemoController {
@GetMapping(version = "1")
public String version1() {
return "API Version 1.0.0";
}
@GetMapping(version = "2")
public String version2() {
return "API Version 2.0.0";
}
}
</code>Version Parsing Configuration: ApiVersionConfigurer
Implement
WebMvcConfigurerand override
configureApiVersioningto customize version resolution, e.g., using request parameters.
<code>@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
// Method 1: request parameter (default "version")
configurer.useRequestParam("version");
// Method 2: request header
// configurer.useRequestHeader("API-Version");
// Method 3: path variable
// configurer.usePathVariable("version");
}
}
</code>Common Version Control Strategies
Request Parameter :
?version=1Request Header :
API-Version: 1Path Variable :
/api/v1/usersAccept Header :
Accept: application/vnd.api+json;version=1Call Test
Testing with request parameters:
<code># Call version 1
curl "http://localhost:8080/?version=1"
# Output: API Version 1.0.0
# Call version 2
curl "http://localhost:8080/?version=2"
# Output: API Version 2.0.0
# No version specified
curl "http://localhost:8080/"
</code>Advanced Configuration: Custom Version Resolver
For complex scenarios, implement a custom
ApiVersionResolverto resolve versions from user‑agent, client IP, or other business rules.
<code>@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useVersionResolver(new ApiVersionResolver() {
@Override
@Nullable
String resolveVersion(HttpServletRequest request) {
// Example 1: from User-Agent
String userAgent = request.getHeader("User-Agent");
if (userAgent != null && userAgent.contains("mobile")) {
return "mobile";
}
// Example 2: based on client IP
String clientIp = getClientIp(request);
if (isTestEnvironment(clientIp)) {
return "beta";
}
// Default version
return "1";
}
});
}
}
</code>Summary
Incoming requests are processed by
RequestMappingHandlerMappingto locate the appropriate controller method.
The handler invokes
ApiVersionStrategyto extract the API version from the request.
After obtaining the version, candidate controller methods are filtered.
The best‑matching method is finally selected and invoked.
Spring Boot 4.0’s native version‑control support enables developers to manage API evolution gracefully, improving maintainability and user experience.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.