Spring @RequestHeader Annotation Example
In this section we will learn about @RequestHeader Annotation.
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.
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 UserController {
@GetMapping("/users")
public User getUser(@RequestHeader String token) {
//TODO
}
}
The following example creates a Spring Boot web application which uses @RequestHeader. The application receives an URL from which it builds a json response to the client.
Project Directory
Pom.xml
<?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>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-requestheader-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-requestheader-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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-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>
User.java
package com.knf.dev.demo.dto;
public class User {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public User(String name, String country) {
this.name = name;
this.country = country;
}
}
UserController.java
package com.knf.dev.demo.controller;
import com.knf.dev.demo.dto.User;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1")
public class UserController {
@GetMapping(value = "/users/{country}/{name}")
public User getUserByCountryAndName
(@PathVariable String country,
@PathVariable(name = "name") String name) {
return new User(name,country);
}
}
Run Application - Application.java
Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.
Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:
mvn spring-boot:run
Try to send a GET request to the /users
endpoint using Postman.
Console Output:
printing the token dsa3dsdsads34dfdf34
Try to send a GET request to the
/headers
endpoint using Postman.