Spring @RequestHeader Annotation example

Let us quickly check how to access http Header information in Spring MVC Controller.


Spring @RequestHeader Annotation


Spring MVC provides annotation @RequestHeader that can be used to map controller parameter to request header value. Following is the simple usage of spring @RequestHeader annotation.



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

@Controller
public class HelloController {

@RequestMapping(value = "/hello.htm")
public String hello(@RequestHeader(value="User-Agent") String userAgent)

//..
}
}

In above code snippet we defined a controller method hello() which is mapped to URL /hello.htm. Also we bind the parameter String userAgent using @RequestHeader annotation. When spring maps the request, it checks http header with name “User-Agent” and bind its value to String userAgent.


If the header value that you specified does not exists in request, Spring will initialize the parameter with null value. In case you want to set default value of parameter you can do so using defaultParameter attribute of spring @RequestHeader annotation.



@RequestMapping(value = "/hello.htm")
public String hello(@RequestHeader(value="User-Agent", defaultValue="foo") String userAgent)

//..
}

Complete Tutorial


Now we know the concept, let us use it and create a Spring MVC application to print value of different http request headers. Following is demo application for Spring Requestheader example.


For this tutorial I will be using following tools and technologies:



  1. Spring MVC 3.2.6.RELEASE

  2. Java 6

  3. Eclipse

  4. Maven 3


Following is the project structure.

spring @requestheader project structure


Create and copy following file contents in the project structure.


Maven configuration: pom.xml



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.viralpatel.spring</groupId>
<artifactId>Spring_Cookie_example</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>SpringMVCCookie</name>
<properties>
<org.springframework.version>3.2.6.RELEASE</org.springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compileSource>1.6</compileSource>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- JSTL taglib -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>springcookie</finalName>
</build>
<profiles>
</profiles>
</project>

Maven configuration is simple. We just need Spring MVC and JSTL dependency.


Deployment description: web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring MVC Http Cookie</display-name>
<welcome-file-list>
<welcome-file>hello.htm</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>

Web.xml is quite simple too. We just need to configure Spring’s DispatcherServlet with *.htm url pattern.


Spring Configuration: spring-servlet.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:annotation-config />
<context:component-scan base-package="net.viralpatel.spring" />


</beans>

In spring-servlet.xml we just defined component scan to load @Controller classes. Note that we are not defining view resolver as we dont need that for this exmaple. But ideally you’ll have one view resolver that maps to JSP views.


Spring Controller: HelloController.java



package net.viralpatel.spring;

import javax.servlet.http.HttpServletResponse;

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

@Controller
public class HelloController {

@RequestMapping(value = "/hello.htm")
public String hello(
@RequestHeader(value="Accept") String accept,
@RequestHeader(value="Accept-Language") String acceptLanguage,
@RequestHeader(value="User-Agent", defaultValue="foo") String userAgent,
HttpServletResponse response) {

System.out.println("accept: " + accept);
System.out.println("acceptLanguage: " + acceptLanguage);
System.out.println("userAgent: " + userAgent);

return null;
}

}

The logic to read http requestheader is written in HelloController. We used spring @RequestHeader annotation to map userAgent, accept, acceptLanguage strings from request header.


Demo


Open your favorite web browser and point to below URL:


http://localhost:8080/Spring_RequestHeader_example/hello.htm


The page will be blank because we haven’t rendered any JSP view. Check the server Console logs. You’ll see the http header values.

spring-requestheader-example-demo


Download Source Code


Spring_RequestHeader_example.zip (8 KB)


References



The post Spring @RequestHeader Annotation example appeared first on ViralPatel.net.








via ViralPatel.net http://feedproxy.google.com/~r/viralpatelnet/~3/vesOgxkVRgA/

Picasa 3.9 Build 137.76

Picasa is software that helps you instantly find, edit and share all the pictures on your PC. Every time you open Picasa, it automatically locates all your pictures (even ones you forgot you had) and sorts them into visual albums organized by date with folder names you will recognize. You can drag and drop to arrange your albums and make labels to ...





via FileHippo.com http://filehippo.com/download_picasa/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

Foxit Reader 6.1.2.1224

Foxit Reader is a free PDF document viewer, with incredible small size, breezing-fast launch speed and rich feature set. Its core function is compatible with PDF Standard 1.7.





via FileHippo.com http://filehippo.com/download_foxit/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

PeaZip 5.2.1

PeaZip is an open source file and archive manager. It's freeware and free of charge for any use.





via FileHippo.com http://filehippo.com/download_peazip/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

ICQ 8.2.6893

With ICQ Instant Messenger you can video/audio chat, send email, SMS and wireless-pager messages, as well as transfer files and URLs. If you're away from your personal computer you can still chat with friends and contacts, even where the ICQ client is not installed, by using the web-based ICQ2Go that works from any computer.





via FileHippo.com http://filehippo.com/download_icq/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

XBMC Media Center 12.3

XBMC is an award winning media center application for Linux, Mac OS X, Windows and XBox. The ultimate hub for all your media, XBMC is easy to use, looks slick, and has a large helpful community. Try it now!





via FileHippo.com http://filehippo.com/download_xbmc_media_center/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

Netgear Nighthawk review: One of the best 802.11ac routers you can buy

8 must-have mobile games for your new smartphone

With these games on your phone, you can easily ignore your obnoxious in-laws!



via PCWorld http://www.techhive.com/article/2080850/8-must-have-mobile-games-for-your-new-smartphone.html#tk.rss_all

Revealed! The best and worst 802.11ac Wi-Fi routers of 2013

Dell Precision T3610 review: A workstation with exceptional fit and finish

25 excellent apps to install on your new Windows tablet

Trendnet TEW-812DRU V2 router review: Barely the basics

Linksys EA6900 802.11ac Wi-Fi router review: Too expensive for what’s delivered

Buffalo WZR-1750DHP 802.11ac Wi-Fi router review: Respectable feature set, mediocre performance

D-Link DIR-868L 802.11ac Wi-Fi router review: Fast and surprisingly inexpensive

Asus RT-AC68U review: The best router on the market, priced accordingly

Alibaba poised to offer telecom services in China

Amazon offers gift cards, shipping refunds for late deliveries

CTIA website shows mobile app data usage

2013 in Video: Microsoft's year in review

The first 3D printed organ, a liver, is expected in 2014

Online holiday shopping up, but not as much as expected, comScore says

The HP Chromebook 11 returns to store shelves

Who blinks first? EU's threat to charge Google over antitrust issues is mostly bluff

Transfer files between Android and Windows devices

Edward Snowden's Christmas message: a child born today will have no conception of privacy

11 must-have apps for your new Windows Phone

Why 2013 was the year of the personal data breach

Macrium Reflect 5.2.6444

A complete disaster recovery solution for your home and office. Protect your personal documents, photos, music and emails. Upgrade your hard disk or try new operating systems in the safe knowledge that everything is securely saved in an easily recovered backup file.





via FileHippo.com http://filehippo.com/download_macrium_reflect/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

Wondershare Video Editor 3.5.0

Thanks to its intuitive UI, Wondershare Video Editor makes creating movies from videos taken via mobile phone, digital camera or camcorder an easy undertaking. After launching the program, the user can choose between two aspect ratios - 16:9 and 4:3. The workspace contains a file manager, a timeline and a preview pane. Once video, image and audio f...





via FileHippo.com http://filehippo.com/download_wondershare_video_editor/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

ZoneAlarm Free 12.0.118.000

ZoneAlarm Free Firewall blocks hackers from infiltrating your home PC by hiding your computer from unsolicited network traffic. By detecting and preventing intrusions, ZoneAlarm Free Firewall keeps your PC free from viruses that slow down performance, and spyware that steals your personal information, passwords, and financial data.





via FileHippo.com http://filehippo.com/download_zonealarm_free/ [[ We are also giving web service. Email:wasim.akh2@gmail.com]]

Microsoft 365 (Office) Insider Preview Build 17628.20006 (Version 2405) Released, Here is What’s New and Fixed

UPDATE: Microsoft 365 Insider (previously known as Office Insider) Preview build 17628.20006 (version 2405) is available for download and in...