java
42 lines · 8 steps
Adding deprecation headers in Spring
A ResponseBodyAdvice that stamps Deprecation and Sunset headers onto responses from endpoints marked as deprecated.
Explained by
highlit
1@ControllerAdvice
2public class DeprecationHeaderAdvice implements ResponseBodyAdvice<Object> {
3
4 private final Clock clock;
5
6 public DeprecationHeaderAdvice(Clock clock) {
7 this.clock = clock;
8 }
9
10 @Override
11 public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
12 return returnType.hasMethodAnnotation(Deprecated.class)
13 || returnType.hasMethodAnnotation(Sunset.class);
14 }
15
16 @Override
17 public Object beforeBodyWrite(Object body,
18 MethodParameter returnType,
19 MediaType selectedContentType,
20 Class<? extends HttpMessageConverter<?>> selectedConverterType,
21 ServerHttpRequest request,
22 ServerHttpResponse response) {
23 HttpHeaders headers = response.getHeaders();
24 headers.add("Deprecation", "true");
25
26 Sunset sunset = returnType.getMethodAnnotation(Sunset.class);
27 if (sunset != null) {
28 OffsetDateTime date = OffsetDateTime.parse(sunset.value());
29 headers.add("Sunset", date.format(DateTimeFormatter.RFC_1123_DATE_TIME));
30 if (!sunset.link().isBlank()) {
31 headers.add("Link", "<" + sunset.link() + ">; rel=\"deprecation\"");
32 }
33 }
34
35 LoggerFactory.getLogger(getClass()).warn(
36 "Deprecated endpoint {} invoked from {}",
37 returnType.getMethod(),
38 request.getRemoteAddress());
39
40 return body;
41 }
42}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1ResponseBodyAdvice lets you rewrite response bodies and headers globally without touching individual controllers.
- 2supports() acts as a filter so the expensive per-response logic only runs where it actually applies.
- 3Driving behavior off method annotations keeps deprecation policy declarative and colocated with the endpoint it describes.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
java
public final class ImportOrganizer { private static final Pattern IMPORT_LINE = Pattern.compile("^import\\s+(static\\s+)?([\\w.]+(?:\\.\\*)?)\\s*;\\s*$");
Sorting Java imports with a regex pass
regex
sorting
text-processing
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/adding-deprecation-headers-in-spring-explained-java-091e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.