这篇文章将为大家详细讲解有关如何在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。