REST (Representational State Transfer) ,表现形式状态转换。
描述 | 传统风格 | REST风格 |
---|---|---|
查询用户 | http://localhost/user/getById?id=1 | http://localhost/user/1 |
保存用户 | http://localhost/user/saveUser | http://localhost/user |
优点:书写简化;隐藏资源的访问行为,无法通过地址得知对资源是何种操作
按照 REST 风格访问资源时使用 行为动作 区分对资源进行了何种操作。
描述 | 访问路径 | 方式 |
---|---|---|
查询全部用户信息 | http://localhost/users | GET (查询) |
查询指定用户信息 | http://localhost/users/1 | GET (查询) |
添加用户信息 | http://localhost/users | POST(新增/保存) |
修改用户信息 | http://localhost/users | PUT(修改/更新) |
删除用户信息 | http://localhost/users/1 | DELETE (删除) |
注:① 上述行为是约定方式,约定不是规范,可以打破,所以称 REST 风格,而不是 REST 规范。
② 描述模块的名称通常使用复数,也就是加 s
的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts…
根据 REST 风格对资源进行访问称为 RESTFUL。
value
(默认):请求访问路径method
:http 请求动作,标准动作(GET/POST/PUT/DELETE)@RequestMapping(value = "/users", method = RequestMethod.POST)
@ResponseBody
public String save(@RequestBody User user){
System.out.println("user save ... " + user);
return "{'module':'user save'}";
}
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("user delete ... " + id);
return "{'module':'user delete'}";
}
@RestController
@RequestMapping("/users")
public class UserController2 {
...
}
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("user getById ... " + id);
return "{'module':'user getById'}";
}
注: @GetMapping(“/{id}”)
的作用相当于 @RequestMapping(value = “/{id}”, method = RequestMethod.GET)
(当注解里面要定义两个属性时,value 这个属性名要写出来),其余注解同理。
REST 风格案例1:
@Controller
public class UserController {
@RequestMapping(value = "/users", method = RequestMethod.POST)
@ResponseBody // 返回 json 形式
public String save(@RequestBody User user){
System.out.println("user save ... " + user);
return "{'module':'user save'}";
}
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("user delete ... " + id);
return "{'module':'user delete'}";
}
@RequestMapping(value = "/users", method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user){
System.out.println("user update ... " + user);
return "{'module':'user update'}";
}
@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id){
System.out.println("user getById ... " + id);
return "{'module':'user getById'}";
}
@RequestMapping(value = "/users", method = RequestMethod.GET)
@ResponseBody
public String getAll(){
System.out.println("user getAll");
return "{'module':'user getAll'}";
}
}
基于案例1的改造:
@RestController
@RequestMapping("/users")
public class UserController2 {
//@RequestMapping(value = "/users", method = RequestMethod.POST)
@PostMapping
public String save(@RequestBody User user){
System.out.println("user save ... " + user);
return "{'module':'user save'}";
}
//@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){
System.out.println("user delete ... " + id);
return "{'module':'user delete'}";
}
//@RequestMapping(value = "/users", method = RequestMethod.PUT)
@PutMapping
public String update(@RequestBody User user){
System.out.println("user update ... " + user);
return "{'module':'user update'}";
}
//@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("user getById ... " + id);
return "{'module':'user getById'}";
}
//@RequestMapping(value = "/users", method = RequestMethod.GET)
@GetMapping
public String getAll(){
System.out.println("user getAll");
return "{'module':'user getAll'}";
}
}
参考链接:黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)