Spring @RequestHeader Annotation with Example

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


@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.

public class HomeController {
	public String testRequestHeader (@RequestHeader String authorization) {

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">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot request header annotation</description>




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;

public class HomeController {
	public String testRequestHeader (@RequestHeader String authorization) {
		System.out.println("printing the auth "+authorization);
		return "Success";
	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


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


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