Sub topic 13
Signalling deprecation and sunset dates
This chapter assumes you have already configured versioning with ApiVersionConfigurer, declared supported versions, and mapped controller methods with the version attribute. Here you will tell clients that a version is on its way out, before you remove it.
Deprecation and sunset are different events
Two terms are easy to mix up:
- Deprecation means a version still works, but clients should move off it.
- Sunset means the date after which the version may stop working.
Spring sends both as HTTP response headers, so a client can detect them without reading your documentation.
| Header | Meaning | Defined in |
|---|---|---|
| `Deprecation` | The version is deprecated, or will be from a given date | RFC 9745 |
| `Sunset` | The version is expected to stop working at this time | RFC 8594 |
| `Link` | Points to documentation, with `rel="deprecation"` or `rel="sunset"` | RFC 8288 |
Registering a deprecation handler
Spring Framework 7 provides the ApiVersionDeprecationHandler interface. The ready-made implementation is StandardApiVersionDeprecationHandler. You configure it per version, then register it on the ApiVersionConfigurer.
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 handler = new StandardApiVersionDeprecationHandler();
handler.configureVersion("1.0")
.setDeprecationDate(ZonedDateTime.parse("2026-09-01T00:00:00Z"))
.setDeprecationLink(URI.create("https://example.org/docs/deprecation"))
.setSunsetDate(ZonedDateTime.parse("2027-03-01T00:00:00Z"))
.setSunsetLink(URI.create("https://example.org/docs/migrate-to-2"));
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "1.1", "2.0")
.setDefaultVersion("1.0")
.setDeprecationHandler(handler);
}
}The handler matches on the version the request resolved to. Any request that resolves to 1.0 gets the extra headers. Requests for 1.1 or 2.0 are unchanged. The handler is separate from your controllers, so you add deprecation signals without editing any mapping.
A response for a deprecated version then carries headers like these:
Deprecation: @1788220800
Sunset: Mon, 01 Mar 2027 00:00:00 GMT
Link: <https://example.org/docs/deprecation>; rel="deprecation"
Link: <https://example.org/docs/migrate-to-2>; rel="sunset"The Deprecation value is a Unix timestamp prefixed with @. The Sunset value is an HTTP date.
What the headers do not do
Setting a sunset date does not retire anything. The version keeps working after that date until you act.
Common mistakes:
- Deprecating a version that is not supported. Configure only versions that appear in
addSupportedVersions. Otherwise the handler never sees them. - Using a past sunset date. Clients may read it as "already gone" while the version still responds.
- Omitting the link. A header with only dates tells clients something is ending but not what to do. Point the
Linkat a migration guide.
Exercise
A client calls your API with version 2.0, and only 1.0 has a deprecation handler configured. Which headers does the response carry, and does a sunset date on 1.0 stop requests for 1.0 on that date?
Show answer
The version 2.0 response carries no Deprecation, Sunset or deprecation Link headers, because the handler matches on the resolved version and only 1.0 is configured. A sunset date on 1.0 does not stop anything. Requests for 1.0 keep working until you remove 1.0 from the supported versions and delete its controller methods.