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.
public Book getHeder(@RequestHeader(value="Username") String username, @RequestHeader(value="Password") String password) { }
@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).
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>requestheaderexample</groupId> <artifactId>requestheaderexample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>requestheaderexample</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
Directory structure.
Book.java
package requestheaderexample; public class Book { int bookId; String bookName; String bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookPrice() { return bookPrice; } public void setBookPrice(String bookPrice) { this.bookPrice = bookPrice; } }
BookController.java
package requestheaderexample; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/rest") public class BookController { @RequestMapping(value = "/getheader",method = RequestMethod.POST) public Book getHeder(@RequestHeader(value="Username") String username, @RequestHeader(value="Password") String password) { System.out.println("username: " + username); System.out.println("password: " + password); Book book = new Book(); if((username.equals("rakesh"))&&(password.equals("kumar"))){ book.setBookId(1); book.setBookName("halfgirlfriend"); book.setBookPrice("100"); } return book; } }
BookMain.java
package requestheaderexample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class BookMain { public static void main(final String[] args) { final ConfigurableApplicationContext configurableApplicationContext = SpringApplication .run(BookMain.class, args); } }
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.
Another example – In each request, we send some information to the server.
Let’s modify the controller and dto class.
HeaderController.java
package requestheaderexample; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/rest") public class HeaderController { @RequestMapping(value = "/getheader",method = RequestMethod.GET) public HeaderDataDTO getHeder(@RequestHeader ("User-Agent") String userAgent, @RequestHeader ("host") String hostName, @RequestHeader ("Accept-Language") String acceptLanguage, @RequestHeader ("Cache-Control") String cacheControl, @RequestHeader ("Cookie") String cookie) { HeaderDataDTO headerDataDTO = new HeaderDataDTO(); headerDataDTO.setAcceptLanguage(acceptLanguage); headerDataDTO.setHostName(hostName); headerDataDTO.setAcceptLanguage(acceptLanguage); headerDataDTO.setCacheControl(cacheControl); headerDataDTO.setCookie(cookie); return headerDataDTO; } }
HeaderDataDTO.java
package requestheaderexample; import org.springframework.web.bind.annotation.RequestHeader; public class HeaderDataDTO { private String userAgent; private String hostName; private String acceptLanguage; private String cacheControl; private String cookie; public String getUserAgent() { return userAgent; } public String getCookie() { return cookie; } public void setCookie(String cookie) { this.cookie = cookie; } public void setUserAgent(String userAgent) { this.userAgent = userAgent; } public String getHostName() { return hostName; } public void setHostName(String hostName) { this.hostName = hostName; } public String getAcceptLanguage() { return acceptLanguage; } public void setAcceptLanguage(String acceptLanguage) { this.acceptLanguage = acceptLanguage; } public String getCacheControl() { return cacheControl; } public void setCacheControl(String cacheControl) { this.cacheControl = cacheControl; } }
Output –
That’s all about @RequestHeader annotation example using Spring Boot.
You may like.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestMapping annotation example In Spring Boot.
- @RequestBody and @ResponseBody annotation example in Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @SpringBootApplication annotation example in Spring Boot.
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @Configuration annotation example using spring boot.
- @ComponentScan example in spring boot.
- Spring transaction management basic.
- Spring transaction management example using spring boot.
- @Transactional REQUIRED vs REQUIRES_NEW example in spring boot.
- What is spring data JPA and what are the benefits.
- Spring Data JPA example using spring boot.
- Spring boot datasource configuration using tomcat.
- Deploy Spring boot war in JBoss EAP server.
- Jboss 7 EPA datasource configuration using oracle and spring boot.
- Spring Boot interceptor example.
- Filter example in Spring Boot.
- JPA EntityManager CRUD example Using Spring Boot.
- How to get JPA EntityManager in Spring Boot.
- Hibernate Eager vs Lazy loading Example.
@RequestHeader annotation docs.