Sub topic 13
Signalling deprecation and sunset dates
Retiring an API version should not surprise anyone. This chapter shows how to make Spring attach standard deprecation and sunset headers to responses for old versions. It also covers how to test those headers and how the headers fit into a retirement timeline. It assumes you have the configuration from the earlier chapters: an ApiVersionConfigurer, a header resolver, supported versions, and controller methods mapped with the version attribute.
Deprecation and sunset are different events
Two words are often used as if they meant the same thing. For versioned APIs they mark two separate moments.
- Deprecation means the version still works, but clients should move off it. The version is on its way out.
- Sunset means the date after which the version may stop working. Clients must have migrated by then.
HTTP has a standard header for each:
| Header | Standard | Meaning | Value format |
|---|---|---|---|
| `Deprecation` | RFC 9745 | The version is deprecated, or will be from a given date | Structured-field date, such as `@1780272000` (seconds since the epoch) |
| `Sunset` | RFC 8594 | The version will stop responding at this time | HTTP date, such as `Tue, 01 Jun 2027 00:00:00 GMT` |
| `Link` | RFC 8288 | Points to documentation, with `rel="deprecation"` or `rel="sunset"` | `<https://...>; rel="deprecation"` |
Clients, API gateways, and monitoring tools can read these headers without any knowledge of your application. A client library can log a warning the first time it sees Deprecation, and a dashboard can count how many calls still hit a version that is about to disappear.
The lifecycle of a version
A version moves through four stages. Spring helps with the middle two by adding headers. You control the first and last by editing the supported versions and the controllers.
Notice that the sunset date is a promise to clients and not an action Spring performs. Spring does not switch a version off when the date passes. You do that by removing the version from the supported list in a later release, as described near the end of this chapter.
Configuring a deprecation handler
Spring Framework 7 lets you register an ApiVersionDeprecationHandler on the ApiVersionConfigurer. The handler runs for each versioned request and may add headers to the response. The framework includes StandardApiVersionDeprecationHandler, which writes the three headers from the table above.
You register it on the same ApiVersionConfigurer you already use. Each version gets its own entry, so you can deprecate 1.0 without touching 1.1.
import java.net.URI;
import java.time.ZonedDateTime;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.accept.StandardApiVersionDeprecationHandler;
import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class ApiVersionConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
StandardApiVersionDeprecationHandler deprecations =
new StandardApiVersionDeprecationHandler();
deprecations.configureVersion("1.0")
.setDeprecationDate(ZonedDateTime.parse("2026-06-01T00:00:00Z"))
.setDeprecationLink(URI.create("https://example.com/docs/api/deprecation"))
.setSunsetDate(ZonedDateTime.parse("2027-06-01T00:00:00Z"))
.setSunsetLink(URI.create("https://example.com/docs/api/migrate-to-2"));
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "1.1", "2.0")
.setDefaultVersion("1.0")
.setDeprecationHandler(deprecations);
}
}Check the exact package and method names against the Spring Framework 7 version your Boot 4 release pulls in. The API is new, and small details can move between releases.
With this configuration, a request that carries X-API-Version: 1.0 gets a response that includes headers like these:
Deprecation: @1780272000
Sunset: Tue, 01 Jun 2027 00:00:00 GMT
Link: <https://example.com/docs/api/deprecation>; rel="deprecation"; type="text/html"
Link: <https://example.com/docs/api/migrate-to-2>; rel="sunset"; type="text/html"A request for 2.0 gets none of them, because no deprecation was configured for that version.
What each setter does
setDeprecationDatesets the moment the version becomes deprecated. It is written to theDeprecationheader as epoch seconds.setDeprecationLinkadds aLinkheader withrel="deprecation". Point it at a page that explains why the version is deprecated.setSunsetDatewrites theSunsetheader. Use the date you are prepared to commit to.setSunsetLinkadds aLinkheader withrel="sunset". Point it at the migration guide.
You can set only some of these. A version with a deprecation date and no sunset date is a valid state: you have told clients to move, but you have not yet chosen a deadline.
Which version triggers the headers
The handler is keyed on the version of the request, after parsing. This has consequences worth knowing before you ship.
- Default versions count. If
1.0is the default version and a client sends no version header, the request is treated as1.0and receives the deprecation headers. Clients that never asked for a version may be the ones most affected, so this is often the right behavior. - Baseline mappings are not the trigger. A method mapped with
2.0+can serve2.0,2.1, and later versions. If you deprecate2.0, only requests that ask for2.0get the headers. Requests for2.1, served by the same method, do not. - Unsupported versions never get here. A request for
0.9is rejected before a handler is chosen, so it receives an error and no deprecation headers. See the earlier chapter on version errors.
The second point is a common source of confusion. Deprecation is a property of a version number as clients see it. It is not a property of a controller method. If one method serves several versions, the same method can answer a deprecated request and a current one.
Writing your own handler
StandardApiVersionDeprecationHandler covers the usual case. You may want your own handler if you need different headers, such as a vendor-specific warning, or if you want to record metrics. In that case, implement ApiVersionDeprecationHandler and register it with setDeprecationHandler. The interface receives the parsed version and the current request and response, so it can add headers or log.
Keep a custom handler cheap. It runs on every versioned request. Avoid database calls or remote lookups here. Read the dates from configuration at startup instead.
Testing the headers
Deprecation headers are part of your API contract, so test them. The same MockMvc approach from the testing chapter works. This test checks that a request for 1.0 is flagged and that a request for 2.0 is not.
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.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class AccountDeprecationTests {
@Autowired
MockMvc mockMvc;
@Test
void version1CarriesDeprecationAndSunsetHeaders() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "1.0"))
.andExpect(status().isOk())
.andExpect(header().exists("Deprecation"))
.andExpect(header().exists("Sunset"))
.andExpect(header().exists("Link"));
}
@Test
void version2CarriesNoDeprecationHeaders() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "2.0"))
.andExpect(status().isOk())
.andExpect(header().doesNotExist("Deprecation"))
.andExpect(header().doesNotExist("Sunset"));
}
}Assert that the headers exist, and avoid asserting the full text of the date. An exact-value assertion breaks when someone moves the sunset date, and moving the date is a normal change that should not fail a test. If you do want to check a value, build the expected string from the same configured date.
A retirement plan
The headers only help if they are part of a longer process. A workable sequence looks like this:
- Announce. Publish the migration guide and set the deprecation date and link. Release this in a normal version of your service.
- Warn. Leave the headers on for a period long enough for clients to notice and react. Months, not days, is typical for external APIs.
- Measure. Count requests per version using logs or metrics. A version still receiving traffic close to the sunset date needs direct contact with its callers.
- Sunset. On the promised date, release a build that no longer lists the version in
addSupportedVersions. Remove or rewrite the controller methods that only served it. - Clean up. Delete the deprecation entry for the removed version.
After step 4, a request for the removed version fails like any other unsupported version, with the error behavior you configured earlier. If your clients expect a clear message, a 410 Gone with an explanation may serve them better than a generic 400. Spring does not do this on its own, so add it through exception handling if you want it.
Common mistakes
- Setting a sunset date and then ignoring it. Clients trust the header. Extending a date is acceptable if you announce it. Silently keeping the version forever teaches clients to ignore the header.
- Deprecating the default version without a plan. Every client that omits the header gets flagged. Decide what the new default will be and when it changes.
- Time zones. Use
ZonedDateTimewith an explicit zone, preferably UTC, so the dates mean the same thing on every server. - Forgetting that the headers need to be read. Spring sends them, but nobody acts on them unless the client logs or alerts. Tell your consumers, in documentation, that the headers exist.
Exercise
Your service supports versions 1.0, 1.1, and 2.0, with 1.0 as the default and a header-based resolver. You deprecate 1.0 with a sunset date. A client sends a request with no version header at all. Does the response carry the Deprecation header, and why?
Show answer
Yes. With no header, Spring applies the default version 1.0 and the deprecation handler sees the request as version 1.0, so it adds the Deprecation and Sunset headers. This is why changing or deprecating the default version affects every client that never sent a version.
Exercise
A method is mapped with version = "2.0+". You configure a deprecation date for version 2.0 only. A client requests version 2.1, which the same method serves. Does the response carry deprecation headers?
Show answer
No. The handler is keyed on the version the client requested. Only requests for 2.0 get the headers, even though the same method answers requests for 2.1.
Exercise
Write the MockMvc assertion that proves a request for version 2.0 is not flagged as deprecated, assuming the usual X-API-Version header.
Show answer
mockMvc.perform(get("/accounts/1").header("X-API-Version", "2.0")).andExpect(header().doesNotExist("Deprecation")).andExpect(header().doesNotExist("Sunset"));