第一類:請求路徑引數
@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;
}
}