统一响应返回工具类

常用返回配置工具类

枚举类

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
@Getter
public enum ResponseCode {

/**
* 枚举为状态码和错误信息
*/
SUCCESS(200, null),

SUCCESS_HEART(202, null),

ERROR(400, "请求错误"),

NO_AUTH(401, "没有权限"),

NOT_FOUND(404, "接口不存在"),

TIME_OUT(408, "请求超时"),

TOKEN_EXPIRED(411, "登录过期"),

USER_ACCOUNT_FREEZE(411, "你的账号已被冻结"),

USER_ACCOUNT_LOGOUT(411, "你的账号已注销"),

DATA_FORMAT_ERROR(412, "数据格式错误"),

SQL_INSERT_ERROR(413, "插入数据异常"),

SQL_UPDATE_ERROR(414, "更新数据异常"),

PHONE_ERROR(400, "手机号码无效"),

PHONE_CODE_EXPIRED(400, "验证码无效或过期"),

PHONE_CODE_ERROR(400, "验证码错误"),

PHONE_HAVE_REGISTERED(400, "手机号码已被注册"),

PARAM_ERROR(400, "参数错误"),

OPERATION_ERROR(400, "操作过于频繁"),

PARAMETRIC_NULL_ANOMALY(412, "参数为空异常"),

USER_ID_ERROR(500, "用户id缺失异常"),

SERVER_ERROR(500, "服务器异常");


Integer code;
String message;

ResponseCode(Integer code, String message) {
this.code = code;
this.message = message;
}
}

响应工具类

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
91
92
93
94
95
96
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {

private Integer code;

private String message;

private T data;

/**
* 成功返回无数据
*
* @return
*/
public static <V> ResponseResult<V> success() {
return new ResponseResult<>(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.message);
}

/**
* 成功返回
*
* @param data 返回数据
* @return
*/
public static <V> ResponseResult<V> success(V data) {
return new ResponseResult<>(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.message, data);
}

/**
* 失败返回
*
* @param errorInfo 错误信息
* @return
*/
public static <V> ResponseResult<V> error(ResponseCode errorInfo) {
return new ResponseResult<>(errorInfo.code, errorInfo.message);
}

/**
* 失败返回
*
* @param str 自定义错误信息
* @return
*/
public static <V> ResponseResult<V> error(String str) {
return new ResponseResult<>(ResponseCode.ERROR.code, str);
}

/**
* 失败返回
*
* @return
*/
public static <V> ResponseResult<V> error(Integer code, String message, V data) {
return new ResponseResult<>(code, message, data);
}

/**
* 失败返回
*
* @return
*/
public static <V> ResponseResult<V> error(ResponseCode responseCode, V data) {
return new ResponseResult<>(responseCode.code, responseCode.message, data);
}

public ResponseResult(ResponseCode responseCode, T data) {
this.code = responseCode.code;
this.message = responseCode.message;
this.data = data;
}

public ResponseResult(T data) {
ResponseCode success = ResponseCode.SUCCESS;
this.code = success.code;
this.message = success.message;
this.data = data;
}

public ResponseResult(Integer code, String message) {
this.code = code;
this.message = message;
}

public ResponseResult(ResponseCode responseCode) {
this.code = responseCode.code;
this.message = responseCode.message;
}

public ResponseResult(ResponseCode responseCode, String message) {
this.code = responseCode.code;
this.message = message;
}
}

相关文章

JWT加密解密工具类

AES加密解密工具类