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.
1 2 3 4 5 6 |
@RequestMapping(value = "/singleHeader") public String testSingleRequestHeader(@RequestHeader("Accept-Encoding") String encoding) { // Your Implemtation } |
@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.
1 2 3 4 5 6 |
@RequestMapping(value = "/singleHeader") public String testSingleRequestHeader(@RequestHeader(value= "Accept-Language" , defaultValue="en-US") String language) { // Your Implemtation } |
Multiple @RequestHeader
We can specify multiple @RequestHeader in the single controller method.
1 2 3 4 5 6 7 |
@RequestMapping(value = "/multipleHeader") public String testMultipleRequestHeader(@RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Accept-Language") String language) { // Your Implementation } |
Map using @RequestHeader
If you specify a Map
1 2 3 4 5 6 |
@RequestMapping(value = "/mapHeader") public String testMapRequestHeader(@RequestHeader Map<String, String> mapHeaders) { // Your Implementation } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.kscodes.sampleproject.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class RequestHeaderController { @RequestMapping(value = "/singleHeader") public String testSingleRequestHeader( @RequestHeader(value = "Accept-Encoding", defaultValue = "en-US") String encoding) { System.out.println("**************************"); System.out.println("Single Request Header Test"); System.out.println("Accept-Encoding :: " + encoding); System.out.println("**************************"); return "success"; } @RequestMapping(value = "/multipleHeader") public String testMultipleRequestHeader(@RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Accept-Language") String language) { System.out.println("**************************"); System.out.println("Multiple Request Header Test"); System.out.println("Accept-Encoding :: " + encoding); System.out.println("Accept-Language :: " + language); System.out.println("**************************"); return "success"; } @RequestMapping(value = "/mapHeader") public String testMapRequestHeader(@RequestHeader Map<String, String> mapHeaders) { System.out.println("**************************"); System.out.println("Map Request Header Test"); for (Map.Entry<String, String> entry : mapHeaders.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } System.out.println("**************************"); return "success"; } } |
Output
When I ran all the 3 URL that are associated with the 3 Controller methods, the server console had all the request headers printed.
http://localhost:8080/sampleproject/singleHeader
http://localhost:8080/sampleproject/multipleHeader
http://localhost:8080/sampleproject/mapHeader