Sub topic 9
Configuring versioning with Spring Boot properties
This chapter assumes you can write @RestController classes with @GetMapping, that you know application.properties, and that you have seen ApiVersionConfigurer in a @Configuration class. Here you move the same setup into properties, and you learn where properties stop being enough.
Why use properties
Earlier you configured versioning in a WebMvcConfigurer. That works, but for the common case it is more code than needed. Spring Boot 4 can build the same setup from three properties. The values live outside the code, so you can change them per environment without recompiling.
The three core properties
The header-based setup from the earlier ApiVersionConfig class looks like this as properties:
spring.mvc.apiversion.use.header=X-API-Version
spring.mvc.apiversion.supported=1.0,1.1,2.0
spring.mvc.apiversion.default=1.0Each line matches one call on ApiVersionConfigurer:
| Property | Equivalent configurer call | Effect |
|---|---|---|
| `spring.mvc.apiversion.use.header` | `useRequestHeader("X-API-Version")` | Reads the version from this request header |
| `spring.mvc.apiversion.supported` | `addSupportedVersions("1.0", "1.1", "2.0")` | Lists the versions the application accepts |
| `spring.mvc.apiversion.default` | `setDefaultVersion("1.0")` | Used when a request carries no version |
With these properties and no WebMvcConfigurer, the AccountController from the previous chapter works unchanged. A request with X-API-Version: 2.0 reaches getV2. A request with no header reaches getV1, because the default is 1.0.
Other ways to read the version
The use group has one property per resolver. You choose the one that matches your strategy.
# Pick one of these, not several
spring.mvc.apiversion.use.header=X-API-Version
# spring.mvc.apiversion.use.query-parameter=version
# spring.mvc.apiversion.use.path-segment=1
# spring.mvc.apiversion.use.media-type-parameter[application/json]=versionThe path segment value is the zero-based index of the segment that holds the version. Path versioning also needs the mapping to include a placeholder for that segment, so check how your controllers declare their paths before you switch.
The exact property names are defined by Spring Boot's WebMvcProperties. Confirm them against the reference documentation for your Boot 4 release. Your IDE's property auto-completion lists them too.
Making versions mandatory
If you leave out spring.mvc.apiversion.default, a request with no version is rejected with a client error. The version is then required. Setting a default makes the version optional, and requests without a version fall back to it.
This is a design choice. A default protects clients that existed before you introduced versioning. A required version forces every new client to say which contract it expects.
Properties or Java configuration
Properties cover the simple cases. Use a WebMvcConfigurer when you need behavior that a property cannot express.
- Properties suit a single resolver, a list of supported versions, and a default.
- Java configuration suits a custom version parser or custom resolver, or setup that depends on other beans.
- Do not define both for the same concern and expect a merge. If you declare your own
configureApiVersioningoverride, test which settings actually apply, and keep the setup in one place.
Common errors
- Mixing up the supported list and the mapping. A version used in
@GetMapping(version = "2.0+")must still be on the supported list. A version not listed is rejected before any mapping is checked. - A trailing space or wrong separator. Write
1.0,1.1,2.0. A typo such as1.0;1.1gives a single unusual version string. - Setting two resolvers. Choose one source for the version. Several sources make it unclear which one wins.
- Expecting a default to make unknown versions work. The default applies only when the version is missing. A request for
3.0is still rejected.
Exercise
Your service has no WebMvcConfigurer. Clients send X-API-Version, and you support 1.0 and 2.0. Old clients send no header and must keep receiving version 1.0 responses. Which properties do you set, and what happens to a request that sends X-API-Version: 3.0?
Show answer
Set spring.mvc.apiversion.use.header=X-API-Version, spring.mvc.apiversion.supported=1.0,2.0 and spring.mvc.apiversion.default=1.0. Requests without the header get version 1.0. A request for 3.0 is not on the supported list, so it is rejected with a client error. The default applies only to missing versions.