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/

No comments:

Post a Comment

If you have any question please let me know

[Changelog] What’s New in Opera 100 and Later Versions

UPDATE: Addition of Opera 109.0 version. In this exclusive changelog article, we are providing information about all versions of Opera web b...