失乡骑士
801.24MB · 2025-12-20
https://google.com/search?q=keyword&page=1
? 和 & 拼接参数/api/users/123
/ 分隔多个参数@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/test")
public String getNoParam() {
return "无参GET请求API接口方法";
}
}
@GetMapping("/user/{id}")
public String getWithPathParam(@PathVariable String id) {
return "RESTful请求传值方法,ID: " + id;
}
访问路径:/api/user/123
@GetMapping("/user")
public String getWithQueryParam(@RequestParam String id) {
return "GET普通请求传值方法,ID: " + id;
}
访问路径:/api/user?id=123
@PostMapping("/save")
public String postMethod(@RequestBody Map<String, Object> map) {
return "POST请求接收成功: " + map;
}
@PutMapping("/update/{id}")
public String putMethod(@PathVariable String id, @RequestBody Map<String, Object> map) {
return "PUT请求接收成功,ID: " + id + ", 数据: " + map;
}
@DeleteMapping("/delete/{id}")
public String deleteMethod(@PathVariable String id) {
return "DELETE请求接收成功,ID: " + id;
}
@RestController: 组合注解,包含 @Controller 和 @ResponseBody@RequestMapping: 类级别的路径映射@GetMapping/@PostMapping/@PutMapping/@DeleteMapping: 方法级别的请求映射@PathVariable: 从路径中获取参数值@RequestParam: 从查询参数中获取值@RequestBody: 从请求体中获取数据Spring MVC 会根据:
自动匹配对应的控制器方法。