Sub topic 3
Configuring Versioning with Spring Boot Properties
Assume you know Spring MVC controllers, HTTP headers, and basic Spring Boot configuration. Spring Boot 4 exposes Spring Framework 7 API versioning through properties, so a simple application needs no custom configuration class.
Configure the supported versions
Add this to application.properties:
spring.mvc.apiversion.use.header=X-API-Version
spring.mvc.apiversion.supported=1.0,1.1,2.0
spring.mvc.apiversion.default=1.0Spring reads the version from X-API-Version. It accepts only the listed versions. A request without the header uses 1.0.
Missing and unsupported versions
The default applies only when the request does not specify a version. A request that specifies an unsupported version cannot select a controller mapping and is rejected, normally with a client error such as 400 Bad Request. Test these cases explicitly because a default can hide clients that have not migrated.
Call and test the endpoint
RestClient can insert the version header for a request:
RestClient client = RestClient.builder()
.baseUrl("http://localhost:8080")
.apiVersionInserter(ApiVersionInserter.useHeader("X-API-Version"))
.build();
String body = client.get()
.uri("/accounts/1")
.apiVersion("2.0")
.retrieve()
.body(String.class);With MockMvc, send the same header and assert the version-specific response:
mockMvc.perform(get("/accounts/1").header("X-API-Version", "2.0"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.firstName").value("Ada"));Deprecate and retire versions
Signal retirement with response headers such as Deprecation: true and Sunset: <HTTP-date>. Document the replacement version and monitor usage before removing the old handler. Keep older versions compatible where possible: add response fields instead of renaming them, and accept old request fields during migration.
Exercise
What happens when a request omits X-API-Version with the properties above?
Show answer
Spring selects the configured default version, 1.0.