一、@RequestHeader 作用
使用該注解可以獲取指定請求頭信息,也可以使用 Map<String,String> 來獲取所有請求頭的 name 和 value
二、@RequestHeader 注解聲明
// 使用 @RequestHeader 注解可以獲取指定的請求頭信息
/**
* Annotation which indicates that a method parameter should be bound to a web request header.
*
* <p>Supported for annotated handler methods in Spring MVC and Spring WebFlux.
*
*/
// 如果想要獲取所有的請求頭信息,可以使用 Map<String,String>、MultiValueMap<String,String>、
// HttpHeaders 這三個 Map 中的任何一個封裝所有請求頭的 name 和 value
/**
* <p>If the method parameter is {@link java.util.Map Map<String, String>},
* {@link org.springframework.util.MultiValueMap MultiValueMap<String, String>},
* or {@link org.springframework.http.HttpHeaders HttpHeaders} then the map is
* populated with all header names and values.
*
* @author Juergen Hoeller
* @author Sam Brannen
* @since 3.0
* @see RequestMapping
* @see RequestParam
* @see CookieValue
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestHeader {
// name 和 value 互為別名,當只有一個參數時,可以省略 value,直接("xxx") 就可以了
/**
* Alias for {@link #name}.
*/
@AliasFor("name")
String value() default "";
/**
* The name of the request header to bind to.
* @since 4.2
*/
@AliasFor("value")
String name() default "";
// 默認情況下,如果請求頭中缺少了指定的 name ,那么將會報錯
/**
* Whether the header is required.
* <p>Defaults to {@code true}, leading to an exception being thrown
* if the header is missing in the request. Switch this to
* {@code false} if you prefer a {@code null} value if the header is
* not present in the request.
* <p>Alternatively, provide a {@link #defaultValue}, which implicitly
* sets this flag to {@code false}.
*/
boolean required() default true;
// 如果請求頭中缺少了指定的 name ,那么會報錯,可以使用 defaultValue 這個屬性指定默認值,就可以避免報錯
// 如果請求頭缺少指定 name ,該屬性設置的值將會作為默認值,如果該屬性不設置值,它有自己的默認值 DEFAULT_NONE
/**
* The default value to use as a fallback.
* <p>Supplying a default value implicitly sets {@link #required} to
* {@code false}.
*/
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
三、@RequestHeader 使用
@RestController
public class RequestParamsController {
@GetMapping("/headParams")
public Map userInfo(
// 由於請求頭中不存在 name=xiaomaomao 這個信息,所以如果只用 value=xiaomaomao 會拋出異常
// 解決方案:
// 1、required 的默認值為 true ,也就是請求頭中沒有 name=xiaomaomao 會報錯,將其值改為 false,即沒有該頭信息也不報錯
// @RequestHeader(value = "xiaomaomao",required = "false") String username
// 2、不修改 required=true 這個默認值,當頭信息中不包含 name=xiaomaomao ,給它一個默認值 hello xiao mao mao
// @RequestHeader(value = "xiaomaomao",defaultValue = "hello xiao mao mao") String username
@RequestHeader(value = "xiaomaomao",defaultValue = "hello xiao mao mao") String username,
// 將請求頭中 name=Accept-Encoding 賦值給形參 encoding
@RequestHeader("Accept-Encoding") String encoding,
// 將請求頭中 name=Host 賦值給形參 host
@RequestHeader("Host") String host,
// 將所有請求頭的 name 和 value 封裝到 Map 集合 headsMap 中
@RequestHeader Map<String,String> headsMap) {
Map map = new HashMap<String, Object>();
map.put("username",username);
map.put("Accept-Encoding",encoding);
map.put("Host",host);
map.put("headsMap",headsMap);
return map;
}
}
四、測試結果
1、請求頭信息如下

2、響應信息如下

