Quick revision
The short answer
In Spring Framework 7 we tell the framework to read the version from the API-Version header, through WebMvcConfigurer.configureApiVersioning or Spring Boot 4 properties, and then declare two handler methods with @GetMapping(path = "/accounts/{id}", version = "1") and version = "2". Unsupported versions are rejected with a 400 error instead of falling through. On older Spring we use the headers attribute of the mapping, a custom RequestCondition, or separate path prefixes; in every case the two shapes are separate DTOs mapped from one domain model.
What to remember
In this answer, we served two shapes of GET /accounts/{id} from one URL. With Spring Framework 7 we configured the API-Version header as the version source, in Java or through Spring Boot 4 properties, set a default for old clients, and declared one method per version with the version attribute; unsupported versions are rejected with a 400 error. On older Spring we used header conditions, a custom RequestCondition or path prefixes. In every case, only the DTOs differ and the service layer stays single.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In Spring Framework 7 we tell the framework to read the version from the API-Version header, through WebMvcConfigurer.configureApiVersioning or Spring Boot 4 properties, and then declare two handler methods with @GetMapping(path = "/accounts/{id}", version = "1") and version = "2". Unsupported versions are rejected with a 400 error instead of falling through. On older Spring we use the headers attribute of the mapping, a custom RequestCondition, or separate path prefixes; in every case the two shapes are separate DTOs mapped from one domain model.
Why this question is asked
Serving two response shapes from one URL is a very common need once an API has real clients: version 1 must keep working while version 2 fixes an awkward design. Spring Framework 7 finally gives us first-class support for this, and interviewers ask about it both to see whether we follow the framework and to see whether we can still solve it on the Spring 6 code most companies run today. In this answer, we will set up versioning with the new support, write the two handler methods, see what happens with missing and unsupported versions, look at the older techniques, and keep the service layer free of duplication.
1A Quick Recap of How Spring Picks a Handler
When a request arrives, Spring MVC's RequestMappingHandlerMapping compares it with every mapping it knows: the path, the HTTP method, and optional conditions such as required headers, parameters and media types. The most specific match wins. Versioning fits naturally into this: the version becomes one more condition that a mapping can declare.
In Spring Framework 7 the version has a dedicated place in that matching. We configure a resolver that reads the version from the request, a parser that turns it into a comparable value, and the set of supported versions. Each mapping then states which version it serves.
2Configuring Where the Version Comes From
We can configure it in Java with the WebMvcConfigurer callback, or in Spring Boot 4 with configuration properties. The property names below match Spring Boot 4 as far as we know, but check the exact names for your version.
Notice the default version. Without it, a request with no header would have no version, and the framework would reject it as missing. Setting a default of 1 means existing clients that never sent a header keep getting the old shape, which is usually what we want when versioning is introduced to an existing API.
3Writing the Two Handler Methods
With the version source configured, the controller simply declares one method per version. Both methods share the path; the version attribute tells them apart.
- Read the API-Version headermissing -> default version 1
- Is the version supported?YesMatch mappings by path and versionNoReject with 400
- Call v1() or v2()both use
AccountService.find - Map the domain object to that version's DTO
AccountV1orAccountV2
4 steps in order. Play them, or step through them one at a time.
The last line is important. If a client sends a version we do not support, the request is rejected with a 400-type error (the framework raises an invalid version exception, check the exact class for your version) rather than silently matching some other method. That is the behaviour we want: a typo in the header should fail loudly instead of returning a shape the client cannot parse. A mapping can also declare a baseline such as version = "1.2+", meaning that version and every later one, so an endpoint that did not change in version 2 does not need a copy.
4Doing It on Older Spring Versions
On Spring Framework 6 and earlier there is no version attribute, but the mapping annotations already support header conditions, which is often enough.
Spring prefers the more specific mapping, so a request with API-Version: 2 goes to v2() and every other request falls back to v1(). The catch is that an unknown version such as 7 also falls back to v1 silently, which is exactly what the new support prevents. If we need strict checking, we can write a custom RequestCondition that parses the header and plug it in by overriding getCustomMethodCondition in our own RequestMappingHandlerMapping, or add a small filter that rejects unknown versions. The simplest alternative of all is separate path prefixes, /v1/accounts and /v2/accounts, perhaps in two controllers, at the cost of changing the URL.
5Keeping the Service Layer Single
Whichever mechanism we use, versions should live only at the edge. Both handlers call the same AccountService and receive the same domain object; only the DTO and its mapping differ. If we instead copied the service or the repository per version, every bug fix would have to be made twice and the versions would drift apart. Keeping each shape as its own record, such as AccountV1 and AccountV2, also makes it obvious in code review which representation a change affects.
6Follow-up questions
How do you announce deprecation of version 1 to clients?
We publish the plan in the documentation and changelog, notify known consumers directly, and add response headers to every version 1 response: a Deprecation header saying it is deprecated, a Sunset header with the date it will stop working, and a Link to the migration guide. Spring Framework 7 can add such headers through a deprecation handler on the versioning configuration (check your version). Then we watch traffic per client on version 1 and contact the ones still calling it as the date approaches.
What happens if a client sends no header?
With a default version configured, the request is treated as that version, so old clients keep working. Without a default, the framework treats the version as required and rejects the request with a 400 error saying it is missing.
Can the same versioning be used in WebFlux?
Yes. Spring Framework 7 offers the same concept for WebFlux through WebFluxConfigurer, and the version attribute works on the same mapping annotations. Spring Boot 4 has matching properties under the WebFlux prefix; check the exact names.
7Conclusion
In this answer, we served two shapes of GET /accounts/{id} from one URL. With Spring Framework 7 we configured the API-Version header as the version source, in Java or through Spring Boot 4 properties, set a default for old clients, and declared one method per version with the version attribute; unsupported versions are rejected with a 400 error. On older Spring we used header conditions, a custom RequestCondition or path prefixes. In every case, only the DTOs differ and the service layer stays single.
Open with
In Spring Framework 7 I would implement WebMvcConfigurer and override configureApiVersioning to read the version from the API-Version header, list the supported versions, and set a default of 1 so existing clients without the header keep working.
Then, if they want more
In Spring Boot 4 the same can be done with properties under spring.mvc.apiversion, though I would check the exact names. Then the controller has two methods on the same path, one with version equals 1 and one with version equals 2.
Spring resolves and parses the header, matches the method, and if the client sends a version we do not support, it rejects the request with a 400 instead of silently matching something else. A mapping can also declare a baseline like 1.2 plus, so unchanged endpoints do not need copies.
On older Spring I would use headers equals API-Version=2 on the version 2 mapping with an unconditioned fallback for version 1, knowing that an unknown version then quietly falls back; if I need strict checking, a custom RequestCondition or a filter does it. Or I would use path prefixes.
Either way both methods call the same service and map one domain object into AccountV1 or AccountV2, so only the representation is duplicated.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 4, Spring Framework 7, Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In Spring Framework 7 and Spring Boot 4 you need GET /accounts/{id} to serve two...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/334edaa73f/