When enabling API versioning using Spring Framework 7 or later, in addition to defining the default version for endpoints that do not have a version defined, you can also define a list of versions supported by your API by using the addSupportedVersions() method when configuring API versioning as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava.springboot; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configureApiVersioning(ApiVersionConfigurer configurer) { configurer.useRequestHeader("X-Api-Version") .addSupportedVersions("5.0.0", "1.0.2") .setDefaultVersion("1.0.0"); } } |
When using this addSupportedVersions() method, in addition to the versions that have been specifically defined for some endpoints, our API will also support the versions declared in the addSupportedVersions() method.
The versions defined in the addSupportedVersions() method will be automatically assigned to endpoints that do not define a version or endpoints that are defined with a baseline version using the “+” sign at the end of the version value, as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.huongdanjava.springboot; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return """ { "message": "Hello from Huong Dan Java!" } """; } @GetMapping(value = "/hello", version = "1.1.0") public String hello1_1_0() { return """ { "message": "Hello from Huong Dan Java! V1.1.0" } """; } @GetMapping(value = "/hello", version = "1.2.0+") public String hello1_2_0() { return """ { "message": "Hello from Huong Dan Java! V1.2.0+" } """; } } |
Versions supported by my API, greater than this baseline version, will be handled by the hello1_2_0() method!
For my example above, the default version will be 1.0.0, supported versions are 5.0.0 and 1.0.2, specific versions are 1.1.0 and 1.2.0. My API will be able to handle the following versions for the “/hello” endpoint: 1.0.0, 1.0.2, 1.1.0, 1.2.0 and 5.0.0.
In ascending order of version, versions 1.0.0 and 1.0.2 will be handled by the hello() method! If you request with version 1.0.0 and 1.0.2, you will see the result as below:

and:

Version 1.1.0 will be handled by the hello_1_1_0() method:

And version 1.2.0, 5.0.0 will be handled by hello1_2_0() method:

If I remove support version 5.0.0 above:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava.springboot; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configureApiVersioning(ApiVersionConfigurer configurer) { configurer.useRequestHeader("X-Api-Version") .addSupportedVersions("1.0.2") .setDefaultVersion("1.0.0"); } } |
then request the “/hello” endpoint again with version 5.0.0, you will see that our API no longer supports version 5.0.0:



