@PathVariable
获取路径参数。即url/{id}这种形式。
GET方式
请求路径:http://localhost:8080/test/id=10
后端接收代码:
@GetMapping("/test/{id}")
public String test(@PathVariable(name = "id") String id){
System.out.println("id:"+id);
return "test:"+id;
}
POST方式
请求路径:http://localhost:8080/testForPost/id=10
后端接收代码:
@PostMapping("/testForPost/{id}")
public String testForPost(@PathVariable(name = "id") String id){
System.out.println("id:"+id);
return "test:"+id;
}
@RequestParam
获取查询参数。即url?name=这种形式
GET方式
请求路径:http://localhost:8080/test1?name=xhw
后端接收代码:
@GetMapping("/test1")
public String test1(@RequestParam(name = "name") String name){
System.out.println("name:"+name);
return "test:"+name;
}
@RequestBody
GET方式
请求路径:http://localhost:8080/bodyTestForGet
后端接收代码:
@GetMapping(path = "/bodyTestForGet")
public String bodyTestForGet(@RequestBody Person person) {
System.out.println(person.toString());
return person.toString();
}