@RequestHeader annotation example using Spring Boot

In this post, we will see @RequestHeader annotation example using Spring Boot.

Basic points about @RequestHeader annotation.

  • This annotation introduced in Spring  3.0, available in org.springframework.web.bind.annotation package.
  • Optional elements  (defaultvalue, name, required, value).
  • We use this annotation as a method parameter.

 

@RequestHeader annotation example using Spring Boot.

prerequisites –

  • JDK 1.8
  • Eclipse
  • maven
  • postman

Create a maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – requestheaderexample, ArtifactId – requestheaderexample and name – requestheaderexample) and click on finish. Keep packaging as the jar.

Modify pom.xml(add a dependency for spring boot).

 

Directory structure.

RequestHeader annotation example using Spring Boot

Book.java

BookController.java

BookMain.java

 

If you encounter any port related issue, define application.properties and change the port.

application.properties

server.port = 9093

Testing using postman.

Run the main class.

RequestHeader annotation example using Spring Boot

Another example – In each request, we send some information to the server.

Let’s modify the controller and dto class.

HeaderController.java

 

HeaderDataDTO.java

 

Output –

RequestHeader annotation example using Spring Boot

That’s all about @RequestHeader annotation example using Spring Boot.

You may like.

@RequestHeader annotation docs.

 

Top