@GetMapping("/getLanguage")
public Result test(@RequestHeader("accept-language") String language) {
// ......
return new Result(true, 600, language);
}
使用postman,没有设置accept-language请求头时,响应:
{
"timestamp": "2019-12-3T20:43:58.971+0000",
"status": 400,
"error": "Bad Request",
"message": "Missing request header 'accept-language' for method parameter of type String",
"path": "/getLanguage"
}
@GetMapping("/num")
public Result getNumber(@RequestHeader("my-number") int myNumber) {
return new Result(true, HttpStatus.OK.value(), String.valueOf(myNumber));
}
使用postman设置my-number请求头值为1,响应:
{
"flag": true,
"code": 200,
"message": "1"
}
(三)一次性获取所有请求头
1、使用Map接收所有请求头
@GetMapping("/getHeaders")
public Result listAllHeaders(@RequestHeader Map<String, String> headers) {
headers.forEach((key, value) -> {
// 日志中输出所有请求头
logger.info(String.format("Header '%s' = %s", key, value));
});
return new Result(true, HttpStatus.OK.value(), "");
}
使用postman请求该地址,控制台打印:
2019-12-03 21:10:35,993 INFO [http-nio-10000-exec-9] com.chushiyan.test.controller.HttpHeaderController: Header 'user-agent' = PostmanRuntime/7.20.1
2019-12-03 21:10:35,994 INFO [http-nio-10000-exec-9] com.chushiyan.test.controller.HttpHeaderController: Header 'accept' = */*
2019-12-03 21:10:35,994 INFO [http-nio-10000-exec-9] com.chushiyan.test.controller.HttpHeaderController: Header 'cache-control' = no-cache
2019-12-03 21:10:35,995 INFO [http-nio-10000-exec-9] com.chushiyan.test.controller.HttpHeaderController: Header 'postman-token' = 47dce6dd-c082-47b0-8867-720e45205aa1
2019-12-03 21:10:35,995 INFO [http-nio-10000-exec-9] com.chushiyan.test.controller.HttpHeaderController: Header 'host' = localhost:10000
2019-12-03 21:10:35,995 INFO [http-nio-10000-exec-9] com.chushiyan.test.controller.HttpHeaderController: Header 'accept-encoding' = gzip, deflate
2019-12-03 21:10:35,996 INFO [http-nio-10000-exec-9] com.chushiyan.test.controller.HttpHeaderController: Header 'connection' = keep-alive
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* 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.
*
* <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 {
/**
* 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 "";
/**
* 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;
/**
* 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;
}
(一)name、value属性
public Result test(@RequestHeader(name="accept-language") String language)
public Result test(@RequestHeader(value="accept-language") String language)
上面这两行代码效果相同。当然都可以省略为:(因为value是可以省略写的)
public Result test(@RequestHeader("accept-language") String language)