REST风格

REST介绍

RESTful介绍

注意事项

  • 上述行为是约定方式,约定不是规范,可以打破,所以称REST风格,而不是REST规范
  • 描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts……

RESTful简单入门

上面我们说到使用http://localhost/users/1这种方式来传值,那后端要怎么判断这个URL是查找还是修改还是删除呢?

答案是:根据请求的的动作来判定的该请求的作用是做什么,比如该请求使用get请求,就是根据id来查找某某用户,如果用delete请求,表示删除该id的用户。

Spring

下面以Spring框架来演示怎么使用RESTful风格

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
39
40
41
42
43
44
45
@Controller
public class UserController {

//设置当前请求方法为POST,表示REST风格中的添加操作
@RequestMapping(value = "/users",method = RequestMethod.POST)
@ResponseBody
public String save(){
System.out.println("user save...");
return "{'module':'user save'}";
}

//设置当前请求方法为DELETE,表示REST风格中的删除操作
//@PathVariable注解用于设置路径变量(路径参数),要求路径上设置对应的占位符,并且占位符名称与方法形参名称相同
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}

//设置当前请求方法为PUT,表示REST风格中的修改操作
@RequestMapping(value = "/users",method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user){
System.out.println("user update..."+user);
return "{'module':'user update'}";
}

//设置当前请求方法为GET,表示REST风格中的查询操作
//@PathVariable注解用于设置路径变量(路径参数),要求路径上设置对应的占位符,并且占位符名称与方法形参名称相同
@RequestMapping(value = "/users/{id}" ,method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id){
System.out.println("user getById..."+id);
return "{'module':'user getById'}";
}

//设置当前请求方法为GET,表示REST风格中的查询操作
@RequestMapping(value = "/users",method = RequestMethod.GET)
@ResponseBody
public String getAll(){
System.out.println("user getAll...");
return "{'module':'user getAll'}";
}
}

注解

@PathVariable

  • 名称:@PathVariable
  • 类型:形参注解
  • 位置:SpringMVC控制器方法形参定义前面
  • 作用:绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应

image-20230413005843692

区别

@RequestParam用于接收URL地址传参或表单传参
@RequestBody用于接收json数据
@PathVariable用于接收路径参数,使用{参数名称}描述路径参数

应用

发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
如果发送非json格式数据,选用@RequestParam接收请求参数
使用URL传一个参数ID,可以采用@PathVariable接收请求路径变量

REST快速开发

冗余的代码

image-20230413010634288

以上截图中的代码和我们之前写的UserController中的方法类似,其中图中两个方法都有三处是有问题的,可以进行优化。存在的问题如下:

问题1:每个方法的@RequestMapping注解中都定义了访问路径/books,重复性太高

问题2:每个方法的@RequestMapping注解中都要使用method属性定义请求方式,重复性太高

问题3:每个方法响应json都需要加上@ResponseBody注解,重复性太高

Rest开发解决以上问题

问题一

Controller类上使用@RequestMapping定义共同的访问路径

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
@Controller
@RequestMapping("/books")
public class BookController {

@RequestMapping(method = RequestMethod.POST)
public String save(@RequestBody Book book){
System.out.println("book save..." + book);
return "{'module':'book save'}";
}
@RequestMapping(value = "/{id}" ,method = RequestMethod.DELETE)
public String delete(@PathVariable Integer id){
System.out.println("book delete..." + id);
return "{'module':'book delete'}";
}
@RequestMapping(method = RequestMethod.PUT)
public String update(@RequestBody Book book){
System.out.println("book update..."+book);
return "{'module':'book update'}";
}
@RequestMapping(value = "/{id}" ,method = RequestMethod.GET)
public String getById(@PathVariable Integer id){
System.out.println("book getById..."+id);
return "{'module':'book getById'}";
}

@RequestMapping(method = RequestMethod.GET)
public String getAll(){
System.out.println("book getAll...");
return "{'module':'book getAll'}";
}
}

问题二

使用@GetMapping、@PostMapping、@PutMapping、@DeleteMapping代替@RequestMapping(method=RequestMethod.XXX)

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
39
@Controller   
@RequestMapping("/books")
public class BookController {

// @RequestMapping( method = RequestMethod.POST)
@PostMapping//使用@PostMapping简化Post请求方法对应的映射配置
public String save(@RequestBody Book book){
System.out.println("book save..." + book);
return "{'module':'book save'}";
}

// @RequestMapping(value = "/{id}" ,method = RequestMethod.DELETE)
@DeleteMapping("/{id}") //使用@DeleteMapping简化DELETE请求方法对应的映射配置
public String delete(@PathVariable Integer id){
System.out.println("book delete..." + id);
return "{'module':'book delete'}";
}

// @RequestMapping(method = RequestMethod.PUT)
@PutMapping //使用@PutMapping简化Put请求方法对应的映射配置
public String update(@RequestBody Book book){
System.out.println("book update..."+book);
return "{'module':'book update'}";
}

// @RequestMapping(value = "/{id}" ,method = RequestMethod.GET)
@GetMapping("/{id}") //使用@GetMapping简化GET请求方法对应的映射配置
public String getById(@PathVariable Integer id){
System.out.println("book getById..."+id);
return "{'module':'book getById'}";
}

// @RequestMapping(method = RequestMethod.GET)
@GetMapping //使用@GetMapping简化GET请求方法对应的映射配置
public String getAll(){
System.out.println("book getAll...");
return "{'module':'book getAll'}";
}
}
  • 名称:@GetMapping、@PostMapping、@PutMapping、@DeleteMapping
  • 类型:方法注解
  • 位置:基于SpringMVCRESTful开发控制器方法定义上方
  • 作用:设置当前控制器方法请求访问路径与请求动作,每种对应一个请求动作,例如@GetMapping对应GET请求
  • 属性:
    value(默认):请求访问路径

问题三

Controller类上使用@RestController注解,等同于@Controller与@ResponseBody两个注解组合功能

1
2
3
4
5
@RestController     //使用@RestController注解替换@Controller与@ResponseBody注解,简化书写
@RequestMapping("/books")
public class BookController {
//方法省略了没写
}
  • 名称:@RestController
  • 类型:类注解
  • 位置:基于SpringMVCRESTful开发控制器类定义上方
  • 作用:设置当前控制器类为RESTful风格,等同于@Controller与@ResponseBody两个注解组合功能

本文章来源于我的博客:https://blog.hikki.site