第一类:请求路径参数

@PathVariable
获取路径参数。即url/{id}这种形式。

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

第二类:Body参数

第三类:请求头参数以及Cookie

@RequestHeader
@CookieValue

代码实现

新建包controller

新建类Student

package com.example.demo.controller;

public class Student {
    private String name = "";
    private int age = 1;

    private int id = 1;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String toString() {
        return "name:" + name + ";age=" + age + ";id:" + id;
    }
}

新建StudentController

package com.example.demo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class StudentController {


    @RequestMapping(value = "/student")

    public Object student(@RequestParam(name = "id") Integer id, Integer age, String name, HttpServletRequest request) {
//      //RequestParam形式不传会报错,Integer id形式不传则为空
        System.out.println(request.getHeader("Connection"));
        System.out.println(request.getCookies());
        for (Cookie cookie : request.getCookies()) {
            if ("myCookie".equals(cookie.getName())) {
                System.out.println(cookie.getValue());
            }
        }

        System.out.println(name);
        Student student = new Student();
        student.setId(id);
        student.setAge(age);
        return student;
    }

    @GetMapping(value = "/student/detail/{id}/{age}")
    public Object student1(@PathVariable("id") Integer id,
                           @PathVariable("age") Integer age) {
        Map<String,Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("age",age);

        System.out.println(retMap);
        return retMap;
    }

    //    @RequestMapping(value = "/student/detail/{id}/{status}")
    @DeleteMapping(value = "/student/detail/{id}/{status}")
    public Object student2(@PathVariable("id") Integer id,
                           @PathVariable("status") Integer status) {
        Map<String,Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("status",status);
        return retMap;
    }

    @DeleteMapping(value = "/student/{id}/detail/{city}")
    public Object student3(@PathVariable("id") Integer id,
                           @PathVariable("city") Integer city) {
        Map<String,Object> retMap = new HashMap<>();

        retMap.put("id",id);
        retMap.put("city",city);
        return retMap;
    }

    @PostMapping(value = "/student/{id}")
    public void addStudent(@RequestBody Student student,@PathVariable("id") Integer id) {
        System.out.println("----------start------");
        System.out.println(student.getName());
        System.out.println(student);
        System.out.println(id);
        System.out.println("----------end------");
    }

    @PostMapping(value = "/student2")
    public Object addStudent2(@RequestBody Student student) {
        System.out.println("----------start------");
        System.out.println(student);
        System.out.println("----------end------");
        return student;
    }

    @PostMapping(value = "/student3")
    public Object addStudent3(Student student) {
//        不写@RequestBody相当于获取类的初始值
        System.out.println("----------start------");
        System.out.println(student);
        System.out.println("----------end------");
        return student;
    }

    @PutMapping(value = "/student/{id}")
    public String updateStudent(@PathVariable("id") Integer id,@RequestBody Student student) {
        System.out.println(student);
        return "update Student ID:" +id;
    }
}


渣渣辉
1.3k 声望147 粉丝