RESTful风格
传统方式
@RequestMapping("/add")
public String test1(int a, int b, Model model) {
int res = a + b;
model.addAttribute("msg", "a + b = " + res);
return "test";
}
url: http://localhost:8080/r/add?a=5&b=2
页面输出:a + b = 7
RESTful风格
@RequestMapping("/add/{a}/{b}")
public String test1(@PathVariable int a, @PathVariable int b, Model model) {
int res = a + b;
model.addAttribute("msg", "a + b = " + res);
return "test";
}
url:http://localhost:8080/r/add/5/2
页面输出:a + b = 7
指定方法
//@RequestMapping(value = "/add/{a}/{b}", method = RequestMethod.GET)
@GetMapping("/add/{a}/{b}")//效果同上
public String test1(@PathVariable int a, @PathVariable String b, Model model) {
String res = a + b;
model.addAttribute("msg", "ab = " + res);
return "test";
}
@PostMapping("/add/{a}/{b}")//实现url复用
public String test2(@PathVariable int a, @PathVariable String b, Model model) {
String res = a + b;
model.addAttribute("msg", "ab = " + res);
return "test";
}
不同方法用不同的注释:
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping