Sub topic 10
What happens with missing or unsupported versions
This chapter assumes you can configure ApiVersionConfigurer with a header resolver and a supported-version list, and that you can map controller methods with the version attribute. It covers what Spring MVC does when a request has no version, a version it does not support, or a version that no handler matches.
Three different failures
Spring checks the version before it picks a handler. The result depends on how the version was resolved.
| Request | Condition | Outcome |
|---|---|---|
| No version sent | A default version is configured | Spring uses the default version |
| No version sent | No default, version required | `MissingApiVersionException`, HTTP 400 |
| Version sent that is not supported | Any configuration | `InvalidApiVersionException`, HTTP 400 |
| Version sent that cannot be parsed | Any configuration | `InvalidApiVersionException`, HTTP 400 |
| Supported version, but no mapping fits | The path matches only other versions | No handler match, so the usual "not found" handling applies |
Both exceptions implement ErrorResponse. With Problem Details enabled, the 400 response body is a ProblemDetail. Without it, you get Spring's default error handling.
Missing versions
A configured default version makes the version optional, because Spring substitutes it for requests that carry none. If you want clients to state a version every time, leave out the default. Then call setVersionRequired(true) to make the intent explicit.
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 StrictVersionConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "2.0")
.setVersionRequired(true);
}
}A request to /accounts/1 without the header now fails with a 400 instead of silently reaching version 1.0.
Unsupported and invalid versions
A request with X-API-Version: 3.0 fails with a 400 when 3.0 is not in the supported set. A request with X-API-Version: banana fails earlier, because the version parser cannot read it. Both cases produce InvalidApiVersionException, so clients can handle them the same way.
Declaring supported versions explicitly matters here. A version you did not declare is rejected immediately, which is clearer for clients than a vague "not found".
Supported but unmatched
A version can pass validation and still reach no handler. Suppose 1.1 is supported but only getV1 (fixed "1.0") and getV2 ("2.0+") exist. A request for 1.1 matches neither: "1.0" is a fixed version, and "2.0+" starts at 2.0. Spring finds no handler, and the request gets the normal no-match response. Fix this by mapping "1.0+" on the older method, or by retiring 1.1 from the supported list.
Testing the failure cases
Check these paths so a configuration change does not quietly alter them.
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class VersionErrorTests {
@Autowired
MockMvc mockMvc;
@Test
void missingVersionIsRejected() throws Exception {
mockMvc.perform(get("/accounts/1"))
.andExpect(status().isBadRequest());
}
@Test
void unsupportedVersionIsRejected() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "3.0"))
.andExpect(status().isBadRequest());
}
}Exercise
Your configuration declares supported versions 1.0 and 2.0, sets no default, and requires a version. What status do you get for a request with no version header, and for a request with header value 2.5?
Show answer
Both return 400 Bad Request. The first raises MissingApiVersionException because no version was sent and none is required to be defaulted. The second raises InvalidApiVersionException because 2.5 is not in the supported set.