How to get access to HTTP header information in Spring MVC REST controller?

SpringRestHttpSpring MvcRequest Headers

Spring Problem Overview


I am new to web programming in general, especially in Java, so I just learned what a header and body is.

I'm writing RESTful services using Spring MVC. I am able to create simple services with the @RequestMapping in my controllers. I need help understanding how to get HTTP header information from a request that comes to my method in my REST service controller. I would like to parse out the header and get some attributes from it.

Could you explain how I go about getting that information?

Spring Solutions


Solution 1 - Spring

When you annotate a parameter with @RequestHeader, the parameter retrieves the header information. So you can just do something like this:

@RequestHeader("Accept")

to get the Accept header.

So from the documentation:

@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
                              @RequestHeader("Keep-Alive") long keepAlive)  {

}

The Accept-Encoding and Keep-Alive header values are provided in the encoding and keepAlive parameters respectively.

And no worries. We are all noobs with something.

Solution 2 - Spring

You can use the @RequestHeader annotation with HttpHeaders method parameter to gain access to all request headers:

@RequestMapping(value = "/restURL")
public String serveRest(@RequestBody String body, @RequestHeader HttpHeaders headers) {
    // Use headers to get the information about all the request headers
    long contentLength = headers.getContentLength();
    // ...
    StreamSource source = new StreamSource(new StringReader(body));
    YourObject obj = (YourObject) jaxb2Mashaller.unmarshal(source);
    // ...
}

Solution 3 - Spring

My solution in Header parameters with example is user="test" is:

@RequestMapping(value = "/restURL")
  public String serveRest(@RequestBody String body, @RequestHeader HttpHeaders headers){

System.out.println(headers.get("user"));
}

Solution 4 - Spring

You can use HttpEntity to read both Body and Headers.

   @RequestMapping(value = "/restURL")
   public String serveRest(HttpEntity<String> httpEntity){
                MultiValueMap<String, String> headers = 
                httpEntity.getHeaders();
				Iterator<Map.Entry<String, List<String>>> s = 
                headers.entrySet().iterator();
				while(s.hasNext()) {
					Map.Entry<String, List<String>> obj = s.next();
					String key = obj.getKey();
					List<String> value = obj.getValue();
				}
				
				String body = httpEntity.getBody();

    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHorse VoiceView Question on Stackoverflow
Solution 1 - SpringVidyaView Answer on Stackoverflow
Solution 2 - SpringDebojit SaikiaView Answer on Stackoverflow
Solution 3 - SpringArmando CordovaView Answer on Stackoverflow
Solution 4 - SpringSreepad ChitragarView Answer on Stackoverflow