REST简介
REST(Representational State Transfer)即表现层状态转移,是一种基于HTTP协议的网络应用程序的架构风格。它强调客户端和服务器之间的交互操作,通过对资源的表现形式进行操作来实现对资源的管理。REST风格的API设计具有简单、灵活、可扩展等特点,因此在Web开发中得到了广泛应用。
优点
- 隐藏资源的访问路径,无法通过地址资源得知对资源是何种操作
- 书写简化
REST风格
按照REST风格访问资源时使用了行为动作区分对资源进行了何种操作
- http://localhost/users 查询全部用户信息 GET(查询)
- http://localhost/users/1 查询指定用户信息 GET(查询)
- http://localhost/users 添加用户信息 POST(新增/保存)
- http://localhost/users 修改用户信息 PUT(修改/更新)
- http://localhost/users/1 删除用户信息 DELETE(删除)
注: 上述行为是约定方式,并不是规范,描述模块的名称通常是复数。
根据REST风格对资源进行访问称为RESTful
RESTful入门案例
截止目前的学习,当前写法为:
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(@RequestBody User user) {
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
@RequestMapping("/delete")
@ResponseBody
public String delete(Integer id) {
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
@RequestMapping("/update")
@ResponseBody
public String update(@RequestBody User user) {
System.out.println("user update..." + user);
return "{'module':'user update'}";
}
@RequestMapping("/getById")
@ResponseBody
public String getById(Integer id) {
System.out.println("user getById..." + id);
return "{'module':'user getById'}";
}
@RequestMapping("/getAll")
@ResponseBody
public String getAll() {
System.out.println("user getAll" );
return "{'module':'user getAll'}";
}
}
REST写法为:
@Controller
public class UserController {
@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'}";
}
@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'}";
}
}
其中:@PathVariable表示路径变量,作用是绑定路径参数与处理器方法形参间的关系(路径参数名要与形参名一致),传入参数时无需向之前?key=value那样,@RequestMapping中value的值也要接上{参数名}用于传递参数
简化开发
在上面的案例中,有许多重复使用的注解
- @RequestMapping将重复的路径提取到类上面,同样@ResponseBody同理。
- @ResponseBody可以和@Controller合并成@RestController。
- @RequestMapping(method = RequestMethod.POST)可以简化成@PostMapping
- @RequestMapping内还有value时也可以进行简化@DeleteMapping("/{id}")
最终的写法如下:
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public String save(@RequestBody User user) {
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id) {
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
@PutMapping
public String update(@RequestBody User user) {
System.out.println("user update..." + user);
return "{'module':'user update'}";
}
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("user getById..." + id);
return "{'module':'user getById'}";
}
@GetMapping
public String getAll() {
System.out.println("user getAll" );
return "{'module':'user getAll'}";
}
}