JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

@RequestHeader in Spring MVC

October 22, 2013 by Krishna Srinivasan Leave a Comment

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.

[code lang=”java”]
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;

@Controller
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";
}
}
[/code]

  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.

[code lang=”java”]
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;
s_sv_p1=1@69@d/30255/29212/29218/29217/29215/29214/29213/29211/29210&s/29216&e/126
User-Agent : Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0
[/code]

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.

Filed Under: Spring Framework Tagged With: Spring Framework, Spring MVC

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved