Spring @RequestHeader Annotation with Example





Hey guys in this post, we will discuss Spring @RequestHeader annotation with example.

Overview


@RequestHeader annotation binds request header values to method parameters. Given below are the available fields that you can pass optionally

  • defaultValue: The default value to use as a fallback.
  • name: The name of the request header to bind to.
  • required: Whether the header is required.
  • value: Alias for name

If the method parameter is Map<String, String>, or HttpHeaders then the map is populated with all header names and values.

@RestController
public class HomeController {
	
	@GetMapping("/test1")
	public String testRequestHeader (@RequestHeader String authorization) {
		//TODO
	}
}

Watch the video


Example on @RequestHeader


Let’s understand @RequestHeader annotation with an example by creating a simple spring boot project

Create spring boot project


There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add the maven dependencies


Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>requestheader</artifactId>
	<version>v1</version>
	<name>requestheader</name>
	<description>Spring boot request header annotation</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.




spring-boot-devtools dependency for automatic reloads or live reload of applications.

Create a Rest controller


Create HomeController.java inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
	
	@GetMapping("/test1")
	public String testRequestHeader (@RequestHeader String authorization) {
		System.out.println("printing the auth "+authorization);
		return "Success";
	}
	
	@GetMapping("/test2")
	public String handleRequestHeader (@RequestHeader Map<String, String> mapValues) {
		System.out.println("printing the header"+mapValues);
		return "Success";
	}
}

The controller contains two handler methods, testRequestHeader() and handleRequestHeader(). The first handler method having the request header name authorization will bind the value to the method argument. whereas, in the second handler method, it binds all the request headers like User-agent, Accept, Accept-encoding everything will bind to the Map.

Run the app


Run the application using the below maven command –

mvn spring-boot:run

Open the rest-client and enter the following URL with the following request body

  • http://localhost:8080/test1

Screenshot-2021-03-12-at-4-13-59-PM

printing the auth: auth value
  • http://localhost:8080/test2

Screenshot-2021-03-12-at-4-16-57-PM

printing the header{authorization=auth value, user-agent=PostmanRuntime/7.26.10, accept=*/*, postman-token=bf3f880c-ad1f-4645-a33f-54a1e848accb, host=localhost:8080, accept-encoding=gzip, deflate, br, connection=keep-alive}




Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply