一.路径参数

路径参数,即浏览器发起请求的URL路径中携带有参数,通过请求URL直接传递参数。这时服务端要接收这些参数,需要用{}来标识该路径参数,并使用@PathVariable来获取路径参数。

package com.gjw.controller;
import com.gjw.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
public class RequestController {
@RequestMapping("/path/{id}")
public String pathParam(@PathVariable Integer id){
@RequestMapping中添加的{...}路径参数必须与@PathVariable后获取的参数的参数名保持一致

这样就可以获取到路径参数”1“了
当有多个路径参数需要获取时,在@RequestMapping与@PathVariable后继续追加即可

package com.gjw.controller;
import com.gjw.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
public class RequestController {
@RequestMapping("/path/{id}/{name}")
public String pathParam(@PathVariable Integer id,@PathVariable String name){
System.out.println(name);
这样就可以获取到路径参数id和name了

总结一下所有类型的参数请求响应
