从零搭建自己的SpringBoot后台框架(三)

释放双眼,带上耳机,听听看~!

一:这么做的好处是什么?

我们调用服务返回的类型有String,List,Map,Bean,Int,Boolean等类型,可以统一成result对象返回,方便项目组其他成员调用接口

二:定义响应码枚举


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
1package com.example.demo.core.ret;
2
3/**
4 * @Description: 响应码枚举,参考HTTP状态码的语义
5 * @author phubing
6 * @date 2019/4/19 09:42
7 */
8public enum RetCode {
9
10   // 成功
11   SUCCESS(200),
12
13   // 失败
14   FAIL(400),
15
16   // 未认证(签名错误)
17   UNAUTHORIZED(401),
18
19   // 接口不存在
20   NOT_FOUND(404),
21
22   // 服务器内部错误
23   INTERNAL_SERVER_ERROR(500);
24
25   public int code;
26
27   RetCode(int code) {
28      this.code = code;
29   }
30}
31

这里简单列出几个,大家可以根据实际业务需求自己定义

三:创建返回对象实体


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
1package com.example.demo.core.ret;
2
3import java.io.Serializable;
4
5import com.alibaba.fastjson.JSON;
6
7/**
8 * @Description:
9 * @author phubing
10 * @date 2019/4/19 09:42
11 */
12public class RetResult<T> {
13
14   public int code;
15
16   private String msg;
17
18   private T data;
19
20   public RetResult<T> setCode(RetCode retCode) {
21      this.code = retCode.code;
22      return this;
23   }
24
25   public int getCode() {
26      return code;
27   }
28
29   public RetResult<T> setCode(int code) {
30      this.code = code;
31      return this;
32   }
33
34   public String getMsg() {
35      return msg;
36   }
37
38   public RetResult<T> setMsg(String msg) {
39      this.msg = msg;
40      return this;
41   }
42
43   public T getData() {
44      return data;
45   }
46
47   public RetResult<T> setData(T data) {
48      this.data = data;
49      return this;
50   }
51
52}
53

说明:code为状态码、msg为提示信息、data为返回的数据

四:转换数据格式


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
1package com.example.demo.core.ret;
2
3/**
4 * @Description:
5 * @author phubing
6 * @date 2019/4/19 09:42
7 */
8public class RetResponse {
9
10   private final static String SUCCESS = "success";
11
12   public static <T> RetResult<T> makeOKRsp() {
13      return new RetResult<T>().setCode(RetCode.SUCCESS).setMsg(SUCCESS);
14   }
15
16   public static <T> RetResult<T> makeOKRsp(T data) {
17      return new RetResult<T>().setCode(RetCode.SUCCESS).setMsg(SUCCESS).setData(data);
18   }
19
20   public static <T> RetResult<T> makeErrRsp(String message) {
21      return new RetResult<T>().setCode(RetCode.FAIL).setMsg(SUCCESS);
22   }
23
24   public static <T> RetResult<T> makeRsp(int code, String msg) {
25      return new RetResult<T>().setCode(code).setMsg(msg);
26   }
27  
28   public static <T> RetResult<T> makeRsp(int code, String msg, T data) {
29      return new RetResult<T>().setCode(code).setMsg(msg).setData(data);
30   }
31}
32

五:功能测试

改造controller代码

改造前代码


1
2
3
4
5
1@PostMapping("/selectById")
2public UserInfo selectById(Integer id){
3    return userInfoService.selectById(id);
4}
5

改造前请求返回数据格式


1
2
3
4
5
1{
2    "id": 1,
3    "userName": "1"
4}
5

改造后代码


1
2
3
4
5
6
1@PostMapping("/selectById")
2public RetResult<UserInfo> selectById(Integer id){
3    UserInfo userInfo = userInfoService.selectById(id);
4    return RetResponse.makeOKRsp(userInfo);
5}
6

改造后请求返回数据格式


1
2
3
4
5
6
7
8
9
1{
2    "code": 200,
3    "msg": "success",
4    "data": {
5        "id": 1,
6        "userName": "1"
7    }
8}
9

怎么样,是不是感觉清爽许多

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++ lambda表达式

2022-1-11 12:36:11

安全经验

优化算法

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索