Spring MVC獲取HTTP請求頭的兩種方式小結

1 前言

請求是任何Web服務要關註的對象,而請求頭也是其中非常重要的信息。本文將通過代碼講解如何在Spring MVC項目中獲取請求頭的內容。主要通過兩種方式獲取:

(1)通過註解@RequestHeader獲取,需要在Controller中顯式獲取;

(2)通過RequestContextHolder獲取,可以任何地方獲取。

接下來通過代碼講解。

2 通過註解@RequestHeader獲取

需要在Controller中顯示使用@RequestHeader。

2.1 獲取某個請求頭

隻獲取其中一個請求頭,相當容易,代碼如下:

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

這裡獲取的是webSite這個請求頭,測試如下:

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

我們查看@RequestHeader的源碼,它還有其它屬性,如下所示:

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屬性默認為true,如果不配置而客戶端沒有傳遞該請求頭,會有報錯。如果配置為false且沒有請求頭,值為null。代碼如下:

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

測試結果如下:

$ 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

defaultValue更容易理解,通過它來設置默認值。如果有傳遞則獲取客戶端傳遞的值,如果沒有傳遞為默認值。代碼如下:

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

測試結果如下:

$ 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 獲取所有請求頭

註解@RequestHeader除瞭可以獲取某個請求頭,還可以一次性獲取所有請求頭,這時不需要指定名字。可以通過Map、MultiValueMap和HttpHeaders來獲取所有請求頭的值,示例代碼如下:

@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();
}

測試如下:

$ 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獲取

通過RequestContextHolder來獲取則可以在其它層獲取。它先獲取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");
}

測試如下:

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

4 總結

以上兩個方法,對於WebFlux而言,通過註解@RequestHeader獲取是可行的;通過RequestContextHolder獲取則不行,可參考【在Spring WebFlux的任何地方獲取Request對象】。

代碼請查看:https://github.com/LarryDpk/pkslow-samples

以上就是Spring MVC獲取HTTP請求頭的兩種方式小結的詳細內容,更多關於Spring MVC獲取HTTP請求頭的資料請關註WalkonNet其它相關文章!

推薦閱讀: