Springboot 2.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 会抛出异常
            // 解决方案:
            // 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、响应信息如下

原文地址:https://www.cnblogs.com/xiaomaomao/p/14289669.html