SpringMVC(六):@RequestMapping下使用@RequestHeader绑定请求报头的属性值、@CookieValue绑定请求中的Cookie值

备注:我本地浏览器的报头(Request Header)信息如下: 

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:JSESSIONID=0BBEA8614215D6E206FB0F66B8DCE3D3; csrftoken=lrfHnPB6TGmnXW9DwWZxIB1ezBgSIuAORWbMaEigY3zExaJo9YHWgdSGtTctr1Zc; Orchrd-=%7B%22Exp-N42-Layouts%22%3A%22open%22%2C%22Exp-N42-Settings%22%3A%22open%22%7D
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

@RequestHeader包含属性值:

--- value :参数名称

--- required :是否必须,默认为true,表示请求参数中必须包含对应的参数,否则抛出异常。

--- defaultValue:当请求参数缺少或者有请求参数但值为空时,值采用该设置值。

请求头包含若个属性,服务器可根据请求头属性信息获取客户端的信息,通过@RequestHeader既可以将请求头的属性值绑定到处理方法的入参中。

示例:

在HelloWord.java中添加方法testRequestHeader函数:

测试1)测试报头参数为key,而且是必须,且该报文参数没有被包含本地报文参数中。

    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "key", required = true) String key) {
        System.out.println("testRequestHeader, key:" + key);
        return SUCCESS;
    }

访问请求地址:http://localhost:8080/SpringMVC_01/class_requestmapping/testRequestHeader

抛出了异常:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String com.dx.springlearn.handlers.HelloWord.testRequestHeader(java.lang.String)]; nested exception is java.lang.IllegalStateException: Missing header 'key' of type [java.lang.String]

测试2)修改测试1)中testRequestHeader代码为如下:

    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "key", required = true, defaultValue = "-999") String key) {
        System.out.println("testRequestHeader, key:" + key);
        return SUCCESS;
    }

访问请求地址:http://localhost:8080/SpringMVC_01/class_requestmapping/testRequestHeader

请求正常响应(无异常抛出),打印信息为:

testRequestHeader, key:-999

测试3)修改testRequestHeader代码为如下:

    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "key", required = true, defaultValue = "-999") String key,
            @RequestHeader(value = "Accept-Encoding") String ae) {
        System.out.println("testRequestHeader, key:" + key + ", Accept-Encoding:" + ae);
        return SUCCESS;
    }

访问请求地址:http://localhost:8080/SpringMVC_01/class_requestmapping/testRequestHeader

请求正常响应(无异常抛出),打印信息为:

testRequestHeader, key:-999, Accept-Encoding:gzip, deflate, sdch, br

 @CookieValue包含属性值:

--- value :参数名称

--- required :是否必须,默认为true,表示请求参数中必须包含对应的参数,否则抛出异常。

--- defaultValue:当请求参数缺少或者有请求参数但值为空时,值采用该设置值。

@CookieValue可让处理方法入参绑定某个cookie值。

测试方法:

    @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID", required = true, defaultValue = "-999") String jsessionId) {
        System.out.println("testCookieValue, JSESSIONID:" + jsessionId );
        return SUCCESS;
    }

测试url:http://localhost:8080/SpringMVC_01/class_requestmapping/testCookieValue

打印结果:testCookieValue, JSESSIONID:A48F72D870ABF4AECDEE1631F3D3F89C

 

 

posted @ 2018-01-04 20:46  cctext  阅读(7221)  评论(0编辑  收藏  举报