To add a custom HTTP header to a Spring MVC response, return a ResponseEntity built with ResponseEntity.ok().header("X-Custom-Header", "value").body(...). For headers that every response needs, use a servlet Filter or a OncePerRequestFilter. For a header on one handler method, inject HttpServletResponse and call addHeader.
The right choice depends on scope: one endpoint, a group of endpoints, or the whole application. This guide covers each approach with current Spring Boot 3 code, the jakarta namespace, and the cases where headers are silently lost.
ResponseEntity is the most explicit option because the status, headers and body live in one object, which also makes the method easy to unit test.
A request to /reports/latest returns:
Use the builder's typed methods for standard headers: contentType, eTag, lastModified, cacheControl, location and allow are all available and validate their values. For several headers at once, build an HttpHeaders object and pass it to ResponseEntity.ok().headers(headers).
When you return a plain object and want a header too, add HttpServletResponse as a method parameter.
setHeader replaces an existing value, and addHeader adds another value with the same name. Set headers before the body is committed. Once the response is flushed, further header changes are ignored without an error.
Older tutorials import javax.servlet. That package applies to Spring Boot 2 and earlier. From Spring Boot 3 (Spring Framework 6), use jakarta.servlet.
A OncePerRequestFilter runs once for each request and works for controllers, static resources and error responses.
Spring Boot registers any Filter bean automatically. Set the header before calling chain.doFilter so that it is present even when the controller commits the response.
If you use Spring Security, remember that it manages many security headers itself (X-Content-Type-Options, Strict-Transport-Security, Cache-Control and others). Configure them through http.headers(...) rather than fighting them in your own filter.
A HandlerInterceptor applies only to requests mapped to MVC handlers, and you can limit it by path.
Set headers in preHandle, not postHandle. By the time postHandle runs, a @ResponseBody response is usually already written and committed, so the header is lost.
If the header depends on the returned value, implement ResponseBodyAdvice. It runs after the controller and before the body is written.
| Need | Best option |
|---|---|
| One endpoint, explicit and testable | ResponseEntity |
| One handler, plain return value | HttpServletResponse parameter |
| Every response, including static content | OncePerRequestFilter |
| Only MVC handlers under certain paths | HandlerInterceptor |
| Header derived from the return value | ResponseBodyAdvice |
For cross-origin requests, JavaScript can read only safelisted headers unless you expose them. Configure CORS with exposedHeaders("X-Total-Count"), for example in CorsRegistry. The header is still visible in the network tab either way.
No. RFC 6648 deprecated the X- prefix convention for new headers. It remains widely used and works fine, but a neutral name such as Request-Id is acceptable for new APIs.
Use a @ControllerAdvice that returns a ResponseEntity with headers, or a servlet filter, which also covers errors produced outside controllers.
No. HTTP header names are case-insensitive, and HttpHeaders treats them that way.
Use ResponseEntity for per-endpoint headers, an HttpServletResponse parameter for quick additions, a OncePerRequestFilter for headers on every response, and ResponseBodyAdvice when the header depends on the body. Set headers before the response is committed, use jakarta.servlet on Spring Boot 3 and later, and expose custom headers through CORS if browser scripts need to read them.