RestFul通用风格案例
# RestFul风格案例
RestFul通用风格
@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。
请求方式 | 请求路径 | 功能 |
---|---|---|
GET | /api/emp/ | 返回员工列表 |
GET | /api/emp/1 | 返回员工编号为1的员工对象 |
POST | /api/emp | 增加一个员工,参数为Emp对象JSON格式 |
PUT | /api/emp/1 | 更新员工,参数empno为1,json格式的员工 |
DELETE | /api/emp/1 | 删除编号为1的员工对象 |
DELETE | /api/emp/ | 删除所有员工 |
# 1、【示例代码】RestFul风格参数传递
@RestController
@RequestMapping("/admin")
public class ArticleController {
@Autowired
private ArticleService articleService;
/**
* @param page
* @param limit
* @param keywords
* @return
* @Author CodeSleep
* @Date: 2021/8/19 21:27
* @Description: //TODO 分页和模糊查询所有文章控制器
* GET http://127.0.0.1:8080/admin/getarticle
*/
@GetMapping("/getarticle")
public ResponseData<ArticleAndTypeDto> getAllArticle(
@RequestParam(name = "page", required = true, defaultValue = "1") int page,
@RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
@RequestParam(name = "keywords", required = true, defaultValue = "") String keywords) {
ResponseData<ArticleAndTypeDto> articleList = articleService.getArticleList(page, limit, keywords);
return articleList;
}
/**
* @param articleID
* @return
* @Author CodeSleep
* @Date: 2021/8/19 21:27
* @Description: //TODO 通过ID删除文章控制器
* DELETE http://127.0.0.1:8080/admin/delarticle/{{articleID}}
*/
@DeleteMapping("/delarticle/{articleID}")
public ResponseData deleteArticleByID(@PathVariable("articleID") int articleID) {
ResponseData responseData = articleService.deleteArticleByID(articleID);
return responseData;
}
/**
* @param articleID
* @return
* @Author CodeSleep
* @Date: 2021/8/20 10:06
* @Description: //TODO 文章批量删除
*/
@DeleteMapping("/delarticle/{articleID}")
public ResponseData deleteAllArticleByID(@PathVariable("articleID") int articleID) {
ResponseData responseData = articleService.deleteArticleByID(articleID);
return responseData;
}
/**
* @param article
* @return
* @Author CodeSleep
* @Date: 2021/8/19 21:28
* @Description: //TODO 增加文章控制器
* POST http://127.0.0.1:8080/admin/addarticle
*/
@PostMapping("/addarticle")
public AjaxOperateResponse addArticle(@RequestBody ArticleEntity article) {
AjaxOperateResponse response = new AjaxOperateResponse(200, articleService.addArticle(article) ? "增加成功" : "增加失败");
return response;
}
/**
* @param articleStatus
* @param articleID
* @return
* @Author CodeSleep
* @Date: 2021/8/20 9:08
* @Description: //TODO 修改文章状态
* PUT http://127.0.0.1:8080/admin/updatest/{{articleStatus}}/{{articleID}}
*/
@PutMapping("/updatest/{articleStatus}/{articleID}")
public ResponseData updateArticleStatus(
@PathVariable("articleStatus") int articleStatus,
@PathVariable("articleID") int articleID) {
ResponseData responseData = articleService.updateArticleStatus(articleID, articleStatus);
return responseData;
}
}
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 2、【示例1】传递参数
@RequestMapping(value="/add3/{userId}/{userName}/a")
public String requestParam3(@PathVariable(value="userId") int userId,@PathVariable("userName") String userName)
{
System.out.println("userId :"+userId+" ,userName :"+userName);
return "add";
}
1
2
3
4
5
6
2
3
4
5
6
说明:当浏览器地址栏输入: http://localhost:8080/SpringMvcDemo03/hc5/add3/888/laowang/a
可以访问到该方法:同时控制台可以得到:
userId
:888
userName
:laowang
即通过访问该方法同时传递参数userId
:888 ;userName
:laowang
# 3、【示例2】传递参数
@PutMapping("/updatest/{articleStatus}/{articleID}")
public ResponseData updateArticleStatus(
@PathVariable("articleStatus") int articleStatus,
@PathVariable("articleID") int articleID) {
ResponseData responseData = articleService.updateArticleStatus(articleID, articleStatus);
return responseData;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
说明:当浏览器地址栏输入: http://localhost:8080/updatest/1/0
可以访问到该方法:同时控制台可以得到:
articleStatus:
0
articleID:
1
即通过访问该方法同时传递参数articleStatus:0
,articleID:1
可修改文章ID为1的文章状态为0
# 4、【示例3】传递参数
@DeleteMapping("/delarticle/{articleID}")
public ResponseData deleteArticleByID(@PathVariable("articleID") int articleID) {
ResponseData responseData = articleService.deleteArticleByID(articleID);
return responseData;
}
1
2
3
4
5
2
3
4
5
说明:当浏览器地址栏输入: http://localhost:8080/delarticle/1
可以访问到该方法:同时控制台可以得到:
articleID:
1
即通过访问该方法同时传递参数articleID:
1,并删除ID为1的这篇文章
编辑 (opens new window)