RESTful风格

一笑奈何 提交于 2020-01-22 00:04:22

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!