第一类:请求路径参数
    1、@PathVariable
    获取路径参数。即url/{id}这种形式。

    2、@RequestParam
    获取查询参数。即url?name=这种形式

    例子
    GET
    http://localhost:8080/demo/123?name=suki_rong
    对应的java代码:

    1. @GetMapping("/demo/{id}")
    2. public void demo(@PathVariable(name = "id") String id, @RequestParam(name = "name") String name) {
    3. System.out.println("id="+id);
    4. System.out.println("name="+name);
    5. }

    输出结果:
    id=123
    name=suki_rong

    第二类:Body参数
    因为是POST请求,这里用Postman的截图结合代码说明

    1、@RequestBody
    例子

    对应的java代码:

    1. @PostMapping(path = "/demo1")
    2. public void demo1(@RequestBody Person person) {
    3. System.out.println(person.toString());
    4. }

    输出结果:
    name:suki_rong;age=18;hobby:programing

    也可以是这样

    1. @PostMapping(path = "/demo1")
    2. public void demo1(@RequestBody Map<String, String> person) {
    3. System.out.println(person.get("name"));
    4. }

    输出结果:
    suki_rong

    2、无注解
    例子

    对应的java代码:

    1. @PostMapping(path = "/demo2")
    2. public void demo2(Person person) {
    3. System.out.println(person.toString());
    4. }

    输出结果:
    name:suki_rong;age=18;hobby:programing

    Person类

    1. public class Person {
    2. private long id;
    3. private String name;
    4. private int age;
    5. private String hobby;
    6. @Override
    7. public String toString(){
    8. return "name:"+name+";age="+age+";hobby:"+hobby;
    9. }
    10. // getters and setters
    11. }

    第三类:请求头参数以及Cookie
    1、@RequestHeader
    2、@CookieValue
    例子
    java代码:

    1. @GetMapping("/demo3")
    2. public void demo3(@RequestHeader(name = "myHeader") String myHeader,
    3. @CookieValue(name = "myCookie") String myCookie) {
    4. System.out.println("myHeader=" + myHeader);
    5. System.out.println("myCookie=" + myCookie);
    6. }

    也可以这样

    1. @GetMapping("/demo3")
    2. public void demo3(HttpServletRequest request) {
    3. System.out.println(request.getHeader("myHeader"));
    4. for (Cookie cookie : request.getCookies()) {
    5. if ("myCookie".equals(cookie.getName())) {
    6. System.out.println(cookie.getValue());
    7. }
    8. }
    9. }