@RequestHeader in Spring MVC

In this tutorial, I would go through an example for how to use @RequestHeader annotation. Every request has a request header part with the set of details sent to the web server. Typically request headers contains the details about browser version, language, what it will accept from the server, etc. are sent to inform the server. This details are many ways useful to the servers for sending back the response. As a developer, we use those details to create the response which is more suitable for that specific request.

Spring MVC provides an annotation @RequestHeader for facilitating use to get the header details easily in our controller class. This annotation would bind the header details with the method arguments, and it can be used inside the methods.

Look at the below picture for the details of header.RequestHeader

Look at the below example.

package javabeat.net;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class RequestHeaderExampleController
    @RequestMapping(value = "/example", method = RequestMethod.GET)
    public String  getHello(@RequestHeader ("host") String hostName,
    		@RequestHeader ("Accept") String acceptType,
    		@RequestHeader ("Accept-Language") String acceptLang,
    		@RequestHeader ("Accept-Encoding") String acceptEnc,
    		@RequestHeader ("Cache-Control") String cacheCon,
    		@RequestHeader ("Cookie") String cookie,
    		@RequestHeader ("User-Agent") String userAgent)
    	System.out.println("Host : " + hostName);
    	System.out.println("Accept : " + acceptType);
    	System.out.println("Accept Language : " + acceptLang);
    	System.out.println("Accept Encoding : " + acceptEnc);
    	System.out.println("Cache-Control : " + cacheCon);
    	System.out.println("Cookie : " + cookie);
    	System.out.println("User-Agent : " + userAgent);
        return "example";
  1. Everything is similar to the writing simple spring mvc example.
  2. @RequestHeader is the annotation which is used in the method arguments to tell that the details are coming from header of that request.
  3. For each details in the header, you have to specify separate @RequestHeader  annotation if you want to used it in your method. As shown in the above example, you can use it for multiple times in the method arguments.

If you run the above code, you would get the following output like, it depends on your environment setup.

Host : localhost:8080
Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept Language : en-US,en;q=0.5
Accept Encoding : gzip, deflate
Cache-Control : max-age=0
Cookie : JSESSIONID=DB1E58823D1E6112A57DFB6366760D9F;
User-Agent : Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0

I hope this article would be useful to understand how to use the @RequestHeader annotation effectively. If you have any questions, please write it in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!