To customize HTTP response headers in Spring, return a ResponseEntity with the headers you need, or take HttpServletResponse as a controller parameter and call setHeader. For headers that every response needs, such as security or tracing headers, register a servlet filter or configure Spring Security instead of repeating the code in each controller.
There is no @ResponseHeader annotation in Spring. The built-in annotation-style options are @ResponseStatus for the status code and @CrossOrigin for CORS headers. The examples use Spring Boot 3, so servlet classes are in jakarta.servlet.
ResponseEntity represents the whole response: status, headers and body. It is the preferred approach for a single endpoint because it is explicit and easy to test.
The builder also has shortcuts for common headers, such as eTag, lastModified, location and contentType. Use add to append a value and set to replace one.
You can declare HttpServletResponse as a method parameter and write headers on it. This is useful when the method returns a plain object and you do not want to wrap it.
Headers must be set before the response is committed. Once the body has been flushed, later setHeader calls are silently ignored. Spring WebFlux has no servlet response, so use ServerHttpResponse.getHeaders() or ServerResponse.ok().header(...) there.
Spring has a type-safe builder for caching headers, which avoids typos in header values.
This sends Cache-Control: max-age=600, public. For endpoints that must never be cached, use CacheControl.noStore(). To support conditional requests, add an ETag, either explicitly with .eTag("\"v1\"") or globally with ShallowEtagHeaderFilter.
A OncePerRequestFilter runs for each request, so it is the right place for headers that should always be present.
If the application uses Spring Security, configure security headers there instead. It already adds X-Content-Type-Options, Cache-Control and X-Frame-Options by default, and you tune them in the HttpSecurity builder:
Cross-origin headers should be configured with CORS support, not set by hand.
For application-wide rules, implement WebMvcConfigurer.addCorsMappings. If you expose custom response headers to browser JavaScript, list them in exposedHeaders, because browsers hide non-standard headers from cross-origin scripts otherwise.
| Need | Best option |
|---|---|
| One endpoint, a few headers | ResponseEntity |
| Headers computed from servlet state | HttpServletResponse parameter |
| Same header on every response | OncePerRequestFilter |
| Security headers | Spring Security headers() configuration |
| Cross-origin access | @CrossOrigin or addCorsMappings |
For cross-origin requests, browsers expose only a few safelisted headers to JavaScript. Add your header to Access-Control-Expose-Headers through the CORS configuration. It is still visible in the network tab.
set replaces any existing value for that name, while add appends another value.
Not reliably. The response is committed once the body is written. Use a filter or ResponseBodyAdvice if you need to adjust headers around the controller.
No. RFC 6648 deprecated the X- prefix convention for new headers, although it remains common in existing APIs.
Use ResponseEntity for per-endpoint headers, HttpServletResponse when you need the servlet object, a filter or Spring Security for headers that apply everywhere, and the CORS support for cross-origin rules. Set headers before the response is committed.