Springboot2.x请求参数之@RequestHeader使⽤⼀、@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 会抛出异常spring framework网络系统参数
// 解决⽅案:
// 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、响应信息如下

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。