Spring MVC @RequestHeader example

Spring MVC has a annotation @RequestHeader that can be used to get the details about the HTTP request headers. Lets see some examples or Spring MVC @RequestHeader.

Single @RequestHeader

@RequestHeader is used with a attribute value to specify the HTTP request header name to be mapped to.

@RequestHeader defaultValue

We can specify a defaultValue to @RequestHeader, in case no header was found with the name specified. In that case the default Value that was given is the value of the RequestHeader variable.

Multiple @RequestHeader

We can specify multiple @RequestHeader in the single controller method.

Map using @RequestHeader

If you specify a Map against a @RequestHeader , it will return you all the HTTP request headers attached to that request.

Spring MVC @RequestHeader test

Lets create a Controller to all the possible scenarios we have discussed and test it by printing the request headers we get.


When I ran all the 3 URL that are associated with the 3 Controller methods, the server console had all the request headers printed.

Spring MVC Sample | Spring MVC @RequestHeader with Example