共计 3605 个字符,预计需要花费 10 分钟才能阅读完成。
我最新最全的文章都在 南瓜慢说 www.pkslow.com,欢送大家来喝茶!
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/p…
欢送关注微信公众号 <南瓜慢说>,将继续为你更新 …
多读书,多分享;多写作,多整顿。