RESTful请求方式
- 查询: GET
- 添加/保存: POST
- 删除: DELET
- 修改: PUT
SSM查询使用RESTful风格
- 无参请求
一般用于查询全部(selectAll)
//设置当前请求方法为GET,表示REST风格中的查询操作 @RequestMapping(value = "/users",method = RequestMethod.GET) @ResponseBody public String getAll(){ System.out.println("user getAll..."); return "{'module':'user getAll'}"; }
- 有参请求
一般用于查询单条信息(selectOne)
//设置当前请求方法为GET,表示REST风格中的查询操作 @RequestMapping(value = "/users/{id}",method = RequestMethod.GET) @ResponseBody public String getUser(@PathVariable Integer id){ System.out.println("user getUser..."); return "{'module':'user getUser'}"; }
SSM添加/保存使用RESTful风格
//设置当前请求方法为POST,表示REST风格中的添加操作 @RequestMapping(value = "/users",method = RequestMethod.POST) @ResponseBody public String saveUser(){ System.out.println("user save..."); return "{'module':'user save'}"; }
SSM删除使用RESTful风格
- 单一参数删除(删除一个)
//设置当前请求方法为DELETE,表示REST风格中的删除操作 @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE) @ResponseBody public String delete(@PathVariable Integer id){ System.out.println("user delete..." + id); return "{'module':'user delete'}"; }
- 多参删除(删除多个)
//设置当前请求方法为DELETE,表示REST风格中的删除操作 @RequestMapping(value = "/users",method = RequestMethod.DELETE) @ResponseBody public String delete(@RequestBody List<Integer> ids){ System.out.println("user delete..." + ids); return "{'module':'user delete'}"; }
SSM修改使用RESTful风格
@RequestMapping(value = "/users",method = RequestMethod.PUT) @ResponseBody public String update(@RequestBody User user){ System.out.println("user update..."+user); return "{'module':'user update'}"; }
进阶简化
@Controller @RequestMapping("/users") public class UserController { @GetMapping @ResponseBody public String getAll() { System.out.println("user getAll..."); return "{'module':'user getAll'}"; } @GetMapping("/{id}") @ResponseBody public String getUser(@PathVariable Integer id) { System.out.println("user getUser..."); return "{'module':'user getUser'}"; } @PostMapping @ResponseBody public String saveUser() { System.out.println("user save..."); return "{'module':'user save'}"; } @DeleteMapping("/{id}") @ResponseBody public String delete(@PathVariable Integer id) { System.out.println("user delete..." + id); return "{'module':'user delete'}"; } @DeleteMapping @ResponseBody public String delete(@RequestBody List<Integer> ids) { System.out.println("user delete..." + ids); return "{'module':'user delete'}"; } @PutMapping @ResponseBody public String update(@RequestBody User user) { System.out.println("user update..." + user); return "{'module':'user update'}"; } }