1. REST简介

REST(Representational State Transfer),表现形式转换

  • 传统风格资源描述形式

    localhost/user/getById?id=1

    localhost/user/saveUser

  • REST风格描述形式

    localhost/users/1

    localhost/user/users

优点

  1. 隐藏资源的访问行为,无法通过地址得知对资源是何种操纵
  2. 书写简化

2. 约定方式

请求路径 行为动作 请求方法
localhost/users 查询全部用户信息 GET(查询)
localhost/users/1 查询指定用户信息 GET(查询)
localhost/users 添加用户信息 POST(新增/保存)
localhost/users 修改用户信息 PUT(修改/更新)
localhost/users/1 删除用户信息 DELETE(删除)

注意事项

上述行为是约定方式,约定不是规范,可以打破,所以称REST风格,而不是REST规范

描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源
例如: users、 books、accounts….

3. 入门案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Controller
public class UserController{

@RequestMapping(value = "/users",method = RequestMethod.POST)
@RessponseBody
public String save(@RequestBody User user){
System.out.println("user save....")
return "{'mpdule':'user save'}"
}

@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@RessponseBody
public String delete(@PathVariable Integer id){
System.out.println("user delete...." + id)
return "{'mpdule':'user delete'}"
}

@RequestMapping(value = "/users",method = RequestMethod.PUT)
@RessponseBody
public String update(@RequestBody User user){
System.out.println("user update....")
return "{'mpdule':'user update'}"
}

@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
@RessponseBody
public String getById(@PathVariable Integer id){
System.out.println("user getById...." + id)
return "{'mpdule':'user getById'}"
}

@RequestMapping(value = "/users",method = RequestMethod.GET)
@RessponseBody
public String getAll(){
System.out.println("user getAll....")
return "{'mpdule':'user getAll'}"
}
}

@RequestBody @RequestParam @PathVariable

区别

  • @RequestBody用于接收json数据
  • @RequestParam用于接收url地址传参或表单传参
  • @PathVariable用于接收路径参数,使用(参数名称)描述路径参数

应用

  • 后期开发中,发送请求参数超过1个时,以json格式为主, @RequestBody应用较广
  • 如果发送非json格式数据,选用@RequestParam接收请求参数
  • 采用RESTful进行开发,当参数数量较少时,例如1个,可以采用@PathVariable接收请求路径变量, 通常用于传递id值

4. 快速开发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@RestController
@RequestMapping("/users")
public class UserController{

@PostMapping
public String save(@RequestBody User user){
System.out.println("user save....")
return "{'mpdule':'user save'}"
}

@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){
System.out.println("user delete...." + id)
return "{'mpdule':'user delete'}"
}

@PutMapping
public String update(@RequestBody User user){
System.out.println("user update....")
return "{'mpdule':'user update'}"
}

@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("user getById...." + id)
return "{'mpdule':'user getById'}"
}

@GetMapping
public String getAll(){
System.out.println("user getAll....")
return "{'mpdule':'user getAll'}"
}
}
  • 名称: @GetMapping @PostMapping @PutMapping @DeleteMapping
  • 类型:方法注解
  • 位置:基于SpringMVC的RESTful1开发控制器方法定义上方
  • 作用:设置当前控制器方法请求访问路径与请求动作,每种对应-个请求动作, 例如@GetMapping对应GET请求