0%

spring boot 学习(二)常用注解开发 RESTful 接口

话不多说直接上代码

ArticleController.java

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
85
86
87
88
89
90
  package com.pjboy.first.controller;

import com.pjboy.first.AjaxResponse;
import com.pjboy.first.model.Article;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
* @program: first
* @description:
* @author: BLADE
* @create: 2020-10-02 19:13
**/
@Slf4j
@RestController
@RequestMapping("/rest")
public class ArticleController {


/**
* @Description: 查询一篇文章
* @Param: [id]
* @return: com.pjboy.first.model.Article
* @Author: BLADE
* @Date: 2020/10/2
*/
//@RequestMapping(value = "/articles/{id}", method = RequestMethod.GET)
@GetMapping("/articles/{id}")
public AjaxResponse getArticle(@PathVariable("id") Long id) {
Article article = Article.builder()
.id(1L)
.author("blade")
.content("xixixiix")
.createTime(new Date())
.title("tit")
.build();
log.info("article:" + article);

return AjaxResponse.success(article);
}

/**
* @Description: 保存一篇文章
* @Param: [id]
* @return: com.pjboy.first.model.Article
* @Author: BLADE
* @Date: 2020/10/2
*/
//@RequestMapping(value = "/articles/", method = RequestMethod.POST)
@PostMapping("/articles/")
public AjaxResponse saveArticle(@RequestBody Article article) {

return AjaxResponse.success();
}

/**
* @Description: 修改一篇文章
* @Param: [id]
* @return: com.pjboy.first.model.Article
* @Author: BLADE
* @Date: 2020/10/2
*/
//@RequestMapping(value = "/articles/", method = RequestMethod.PUT)
@PutMapping("/articles/")
public AjaxResponse updateArticle(@RequestBody Article article) {

//if (article.getId() == null) {
//TODO 抛出一个自定义的异常
//}

return AjaxResponse.success();
}

/**
* @Description: 删除一篇文章
* @Param: [id]
* @return: com.pjboy.first.model.Article
* @Author: BLADE
* @Date: 2020/10/2
*/
//@RequestMapping(value = "/articles/{id}", method = RequestMethod.DELETE)
@DeleteMapping("/articles/{id}")
public AjaxResponse deleteArticle(@PathVariable("id") long id) {
log.info("deleteArticle:" + id);
return AjaxResponse.success();
}

}

AjaxResponse.java

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
package com.pjboy.first;

import lombok.Data;

/**
* @program: first
* @description:
* @author: BLADE
* @create: 2020-10-02 19:19
**/
@Data
public class AjaxResponse {
private boolean isok;
private int code; // 200、400、500
private String message;
private Object data;
public AjaxResponse() {

}

public static AjaxResponse success() {
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse.setMessage("请求相应成功!");
return ajaxResponse;
}

public static AjaxResponse success(Object obj) {
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse.setMessage("请求相应成功!");
ajaxResponse.setData(obj);
return ajaxResponse;
}
public static AjaxResponse success(Object obj, String message) {
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse.setMessage(message);
ajaxResponse.setData(obj);
return ajaxResponse;
}

}