JavaWeb博客系统后端-重构ResponseResult

# JavaWeb博客系统后端-系列

JavaWeb博客系统后端-功能需求 (opens new window)

JavaWeb博客系统后端-项目创建 (opens new window)

JavaWeb博客系统后端-封装返回结果 (opens new window)

前面我们编写了ResponseResult,作为我们返回的类,里面可以封装请求是否成功,状态码和数据信息

一般来说,我们为了方便使用,快速创建,我们会编写几个快速创建的方法。

比如说:

  • SUCCESS() 成功
  • FAILED() 失败
  • PERMISSION_DENINE() 权限不够
  • UN_LOGIN() 未登录
  • PARAMS_ILL() 参数错误

# 通过枚举表示各种情况

package net.sunofbeaches.response;

public interface IResponseState{

    String getMessage();

    boolean isSuccess();

    int getCode();
}

1
2
3
4
5
6
7
8
9
10
11

定义一个接口供外面使用

package net.sunofbeaches.response;

public enum ResponseState implements ICommentResult {
    SUCCESS(10000, true, "操作成功"),
    FAILED(20000, false, "操作失败"),
    PARAMS_ILL(30000, false, "参数错误"),
    PERMISSION_DENIED(40000, false, "权限不够"),
    NOT_LOGIN(50000, false, "账号未登录")LOGIN_SUCCESS(60000,true, "登录成功");


    int code;
    boolean isSuccess;
    String message;

    ResponseState(int code, boolean isSuccess, String message) {
        this.code = code;
        this.isSuccess = isSuccess;
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public boolean isSuccess() {
        return isSuccess;
    }

    @Override
    public int getCode() {
        return code;
    }

}
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

# 重构ResponseResult

package net.sunofbeaches.response;

public class ResponseResult {

    public ResponseResult(ICommentResult commentResult) {
        this.message = commentResult.getMessage();
        this.success = commentResult.isSuccess();
        this.code = commentResult.getCode();
    }

    public static ResponseResult SUCCESS() {
        return new ResponseResult(CommentResponse.SUCCESS);
    }

    public static ResponseResult SUCCESS(String message) {
        ResponseResult success = SUCCESS();
        success.setMessage(message);
        return success;
    }

    public static ResponseResult FAILED() {
        return new ResponseResult(CommentResponse.FAILED);
    }

    public static ResponseResult FAILED(String message) {
        ResponseResult failed = FAILED();
        failed.setMessage(message);
        return failed;
    }

    private String message;
    private boolean success;
    private int code;
    private Object data;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = 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
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

如果后面有需要,再添加即可!按套路出牌就好!

上次更新: 2022/03/28, 23:04:38