# Two ways to get the http request headers in Spring MVC

# 1 Introduction

HTTP request headers are very important to all Web applications, this article will introduce two ways to get the http request headers in Spring MVC. First

  • (1) Annotation @RequestHeader in Controller
  • (2) RequestContextHolder anywhere

# 2 Annotation @RequestHeader

This annotation should be used in Controller.

# 2.1 Get one request header

if just get one header, it's quite easy.

@GetMapping("/webSite")
public String webSite(@RequestHeader("webSite")String webSite) {
  return "The webSite is " + webSite;
}

The code is used to get the header webSite, test as below:

$ curl http://localhost:8088/header/webSite -H 'webSite: www.pkslow.com'
The webSite is www.pkslow.com

By checking the source code of @RequestHeader, it has other properties:

public @interface RequestHeader {
    @AliasFor("name")
    String value() default "";
    @AliasFor("value")
    String name() default "";
    boolean required() default true;
    String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}

# 2.1.1 required

required default value is true, if the client didn't pass the header, it will cause an Exception. If required is false and no header passed, we will get a null value by the annotation.

@GetMapping("/webSite-not-required")
public String webSiteNotRequired(@RequestHeader(value = "webSite", required = false)String webSite) {
  return "The webSite is " + webSite;
}

Test as below:

$ curl http://localhost:8088/header/webSite-not-required -H 'webSite: www.pkslow.com'
The webSite is www.pkslow.com

$ curl http://localhost:8088/header/webSite-not-required
The webSite is null

# 2.1.2 defaultValue

If the client didn't pass the header, it will get the defaultValue:

@GetMapping("/webSite-defaultValue")
public String webSiteDefaultValue(@RequestHeader(value = "webSite", defaultValue = "pkslow.com")String webSite) {
  return "The webSite is " + webSite;
}

Test:

$ curl http://localhost:8088/header/webSite-defaultValue -H 'webSite: www.pkslow.com'
The webSite is www.pkslow.com

$ curl http://localhost:8088/header/webSite-defaultValue
The webSite is pkslow.com

# 2.2 Get all headers

Beside one header, the annotation @RequestHeader can get all headers in one go. We don't need to specify the header name. The headers can be stored in Map, MultiValueMap and HttpHeaders. The code as below:

@GetMapping("/allMap")
public Map<String, String> allMap(@RequestHeader Map<String, String> headers) {
  return headers;
}

@GetMapping("/allMultiValueMap")
public Map<String, String> allMultiValueMap(@RequestHeader MultiValueMap<String, String> headers) {
  return headers.toSingleValueMap();
}

@GetMapping("/allHttpHeaders")
public String allHttpHeaders(@RequestHeader HttpHeaders headers) {
  return headers.toString();
}

Test:

$ curl http://localhost:8088/header/allMap -H 'Authorization: Basic cGtzbG93OjEyMzQ1Ng==' -H 'webSite: www.pkslow.com'
{"host":"localhost:8088","user-agent":"curl/7.64.1","accept":"*/*","authorization":"Basic cGtzbG93OjEyMzQ1Ng==","website":"www.pkslow.com"}

$ curl http://localhost:8088/header/allMultiValueMap -H 'Authorization: Basic cGtzbG93OjEyMzQ1Ng==' -H 'webSite: www.pkslow.com'
{"host":"localhost:8088","user-agent":"curl/7.64.1","accept":"*/*","authorization":"Basic cGtzbG93OjEyMzQ1Ng==","website":"www.pkslow.com"}

$ curl http://localhost:8088/header/allHttpHeaders -H 'Authorization: Basic cGtzbG93OjEyMzQ1Ng==' -H 'webSite: www.pkslow.com'
[host:"localhost:8088", user-agent:"curl/7.64.1", accept:"*/*", authorization:"Basic cGtzbG93OjEyMzQ1Ng==", website:"www.pkslow.com"]

# 3 RequestContextHolder

We don't have to fetch the headers in Controller with the RequestContextHolder. But it doesn't get the header directly. It will get the Request first, and then get the headers from the Request.

@GetMapping("/webSite-RequestContextHolder")
public String webSiteRequestContextHolder() {
  ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
  // get the request
  HttpServletRequest request = requestAttributes.getRequest();
  return "The webSite is " + request.getHeader("webSite");
}

Test:

$ curl http://localhost:8088/header/webSite-RequestContextHolder -H 'webSite: www.pkslow.com'
The webSite is www.pkslow.com

# 4 WebFlux

For WebFlux, @RequestHeader is ok, but the RequestContextHolder doesn't work. More details can check: Get Request Object anywhere in Spring WebFlux (opens new window).

Show you the code: https://github.com/LarryDpk/pkslow-samples

Last Updated: 8/18/2023, 11:39:36 PM