暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringMVC中用于接收请求数据的注解

凯哥学堂 2018-12-10
379


声明:本栏目所使用的素材都是凯哥学堂VIP学员所写,学员有权匿名,对文章有最终解释权;凯哥学堂旨在促进VIP学员互相学习的基础上公开笔记。

SpringMVC中用于接收请求数据的注解

1.@RequestHeader注解,该注解用于处理request中的header部分,也就是http请求头的部分,它可以把header部分的值绑定到方法的参数上,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
    @RequestMapping("test.do")
    public void method(
            @RequestHeader("Host") String host,
            @RequestHeader("Content-Type") String contentType
    ){
        System.out.println(host);
        System.out.println(contentType);
    }
}

使用Postman进行访问,访问方式如下:

控制台打印结果:

localhost:8080
application/json

从打印结果可以看到,以上的代码把http请求头中的Host以及Content-Type字段的值,都绑定到了注解配置的方法参数上,这就是@RequestHeader注解的作用。

2.@CookieValue注解,该注解用于把http请求头中关于cookie的值绑定到方法的参数上,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
    @RequestMapping("test.do")
    public void method(
            @CookieValue("JSEESIONID") String cookie
    ){
        System.out.println(cookie);
    }
}

使用Postman进行访问,访问方式如下:

控制台打印结果:

415A4AC178C59DACE0B2C9CA727CDD84

如上,@CookieValue注解帮我们把Cookie里JSEESIONID的值绑定到了该方法的参数中。

3.@PathVariable注解, 该注解可以把@RequestMapping注解中配置的URL占位符映射的值,绑定到相应的方法参数上,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
    // 将URL上映射的值绑定到方法参数上,用 {} 来进行绑定
    @RequestMapping("test.do/{typeid}/{id}")
    public void method(
            @PathVariable String typeid,
            @PathVariable String id
    ) {
        System.out.println(typeid);
        System.out.println(id);
    }
}

使用Postman进行访问,访问方式如下:

控制台打印结果:

8451
25
4.@RequestBody注解,该注解常用来处理application/json, application/xml等数据,也就是用于处理http请求体的内容。通过这个注解可以很轻松的获取到请求体的数据,再也不用像使用Servlet时那样通过流去读了,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/test")
public class Test {
    @RequestMapping("/test.do")
    public void method(
            @RequestBody String json
    ) {
        System.out.println(json);
    }
}

使用Postman进行访问,访问方式如下:

控制台打印结果:

{
    "name" : "Jon",
    "age" : 23,
    "address" : "usa"
}
5.@SessionAttributes注解,该注解用来将方法参数中的值绑定到HttpSession的attribute中,这个注解是写在类上的,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Controller
@SessionAttributes("name")
@RequestMapping("/test")
public class Test {
    @RequestMapping("/test.do")
    public void method(Map<String, Object> map) {
        // 由于@SessionAttributes注解的作用,这对键值会被存储一份到HttpSession对象的attribute中
        map.put("name", "Jon");
    }
}

该注解有value、types两个属性,可以通过名字和类型来指定需要存储到HttpSession中的数据;示例:

package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Controller
@SessionAttributes(value = "name", types = Object.class)
@RequestMapping("/test")
public class Test {
    @RequestMapping("/test.do")
    public void method(Map<String, Object> map) {
        // 由于@SessionAttributes注解的作用,这对键值会被存储一份到HttpSession对象的attribute中
        map.put("name", "Jon");
    }
}
6.@SessionAttribute注解,该注解用来访问预先存在的HttpSession中的属性值,例如HttpSession中存储了一个name的值,通过@SessionAttribute注解可以取出这个name的值,并且可以绑定到方法的参数上,所以这个注解是写在方法参数上的,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/test")
public class Test {
    @RequestMapping("/test.do")
    // 拿出HttpSession中键为name的值,绑定到方法参数name上
    public void method(@SessionAttribute("name") String name) {
    }
}
7.@ModelAttribute注解,该注解有两个用法,一个是用于方法上,一个是用于参数上:
  • 用于方法上时: 通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;
  • 用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上;
    要绑定的值来源于:
    • @SessionAttributes 启用的attribute 对象上;
    • @ModelAttribute 用于方法上时指定的model对象;

上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。
用在方法上的示例代码:

package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/test")
public class Test {
    @ModelAttribute
    @RequestMapping("/test.do")
    public Account method(@RequestParam String number) {
        return accountManager.findAccount(number);
    }
}

用在参数上的示例代码:

package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/test")
public class Test {
    @RequestMapping("/test.do")
    public void method(@ModelAttribute String name) {
    }
}


文章转载自凯哥学堂,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论