您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何在Spring MVC中獲取HTTP請求頭,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
(2)通過RequestContextHolder獲取,可以任何地方獲取。
接下來通過代碼講解。
需要在Controller中顯示使用@RequestHeader。
只獲取其中一個請求頭,相當容易,代碼如下:
@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
注解@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]
通過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
關于如何在Spring MVC中獲取HTTP請求頭就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。