@RequestHeader binds an HTTP request header to a parameter of a Spring MVC or WebFlux handler method. Instead of reading headers from HttpServletRequest, you declare the header you need and Spring extracts it, converts it to the parameter type, and fails the request with a 400 error if a required header is missing.
Typical uses are reading Authorization, Accept-Language, User-Agent, correlation IDs and API version headers. It is the header counterpart of @RequestParam and @PathVariable.
The annotation takes the header name. Names are case-insensitive, as HTTP requires.
If you compile with the -parameters flag (Spring Boot's Maven and Gradle plugins do this), you can omit the name when the parameter name matches the header after conversion, but writing the header name explicitly is clearer and safer.
By default the header is required. If it is absent, Spring returns 400 Bad Request. You can relax that with required or defaultValue.
Setting defaultValue makes the parameter optional automatically. For optional headers you can also use java.util.Optional<String>.
Header values are strings, and Spring converts them to the declared type with its conversion service.
A non-numeric value raises a type mismatch and results in a 400 response.
To inspect every header, bind a Map<String, String>, a MultiValueMap<String, String>, or an HttpHeaders object.
| Parameter type | Behaviour |
|---|---|
Map<String, String> | One value per header name, the first value is used |
MultiValueMap<String, String> | All values for repeated headers |
HttpHeaders | Case-insensitive, with helper methods such as getContentType() |
HttpHeaders is usually the best choice, because lookups ignore case and you get typed accessors.
For headers that may repeat or contain a comma-separated list, such as Accept, bind a List.
Authorization, but prefer Spring Security to parse and validate credentials. Do not log header values that carry tokens.X-Request-Id or the W3C traceparent header through your logs. With Micrometer Tracing this happens automatically.Accept-Language is better handled by a LocaleResolver than by manual header parsing.If-None-Match and If-Modified-Since are handled by WebRequest.checkNotModified or ResponseEntity helpers.X-Forwarded-For unless your proxy sets them and you configure Spring to use them.For setting headers on the response, return a ResponseEntity. @RequestHeader only reads request headers.
The header method on the request builder sets the header, so you can test both the present and the missing cases.
Yes. A missing required header results in a 400 Bad Request. Set required = false or provide a defaultValue to make it optional.
No. HTTP header names are case-insensitive, and Spring matches them that way.
@RequestHeader reads from HTTP headers, while @RequestParam reads query string or form parameters.
Yes. Annotated controllers in Spring WebFlux support @RequestHeader with the same attributes.
@RequestHeader gives handler methods typed access to request headers with built-in conversion, required checks and default values. Bind one header by name, or all headers with HttpHeaders. Handle credentials and locale with the dedicated Spring features, and be careful with forwarding headers. See the Spring Framework reference on @RequestHeader for the full list of options.