1
|
package com.sunyo.wlpt.message.bus.service.response;
|
1
|
package com.sunyo.wlpt.message.bus.service.response;
|
2
|
|
2
|
|
|
|
3
|
+import com.sunyo.wlpt.message.bus.service.exception.CustomException;
|
|
|
4
|
+import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
|
3
|
import lombok.Data;
|
5
|
import lombok.Data;
|
4
|
|
6
|
|
5
|
import java.io.Serializable;
|
7
|
import java.io.Serializable;
|
|
@@ -46,25 +48,76 @@ public class ResultJson<T> implements Serializable { |
|
@@ -46,25 +48,76 @@ public class ResultJson<T> implements Serializable { |
46
|
}
|
48
|
}
|
47
|
|
49
|
|
48
|
/**
|
50
|
/**
|
49
|
- * 有参,构造方法;
|
|
|
50
|
- * 重载
|
51
|
+ * 定义有参构造器
|
|
|
52
|
+ *
|
|
|
53
|
+ * @param code 响应状态
|
|
|
54
|
+ * @param msg 响应消息
|
51
|
*/
|
55
|
*/
|
52
|
- public ResultJson(String code) {
|
|
|
53
|
- this.code = code;
|
|
|
54
|
- }
|
|
|
55
|
-
|
|
|
56
|
- public ResultJson(T data) {
|
|
|
57
|
- this.data = data;
|
|
|
58
|
- }
|
|
|
59
|
-
|
|
|
60
|
public ResultJson(String code, String msg) {
|
56
|
public ResultJson(String code, String msg) {
|
61
|
this.code = code;
|
57
|
this.code = code;
|
62
|
this.msg = msg;
|
58
|
this.msg = msg;
|
63
|
}
|
59
|
}
|
64
|
|
60
|
|
|
|
61
|
+ /**
|
|
|
62
|
+ * 定义有参构造器
|
|
|
63
|
+ *
|
|
|
64
|
+ * @param code 响应状态
|
|
|
65
|
+ * @param msg 响应消息
|
|
|
66
|
+ * @param data 响应数据
|
|
|
67
|
+ */
|
65
|
public ResultJson(String code, String msg, T data) {
|
68
|
public ResultJson(String code, String msg, T data) {
|
66
|
this.code = code;
|
69
|
this.code = code;
|
67
|
this.msg = msg;
|
70
|
this.msg = msg;
|
68
|
this.data = data;
|
71
|
this.data = data;
|
69
|
}
|
72
|
}
|
|
|
73
|
+
|
|
|
74
|
+ /**
|
|
|
75
|
+ * 定义静态、成功方法(重载)
|
|
|
76
|
+ *
|
|
|
77
|
+ * @return 成功(没有响应数据)
|
|
|
78
|
+ */
|
|
|
79
|
+ public static ResultJson success() {
|
|
|
80
|
+ return new ResultJson<>("200", "success");
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ /**
|
|
|
84
|
+ * 定义静态、成功方法(重载)
|
|
|
85
|
+ *
|
|
|
86
|
+ * @return 成功(响应数据)
|
|
|
87
|
+ */
|
|
|
88
|
+ public static ResultJson success(Object data) {
|
|
|
89
|
+ return new ResultJson<>("200", "success", data);
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ /**
|
|
|
93
|
+ * 请求出现异常时的响应数据封装
|
|
|
94
|
+ *
|
|
|
95
|
+ * @param e 自定义异常类
|
|
|
96
|
+ * @return 返回异常信息
|
|
|
97
|
+ */
|
|
|
98
|
+ public static ResultJson error(CustomException e) {
|
|
|
99
|
+ ResultJson result = new ResultJson<>();
|
|
|
100
|
+ result.setCode(e.getCode());
|
|
|
101
|
+ if (e.getCode() == CustomExceptionType.CLIENT_ERROR.getCode()) {
|
|
|
102
|
+ result.setMsg(e.getMessage());
|
|
|
103
|
+ } else if (e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()) {
|
|
|
104
|
+ result.setMsg(e.getMessage() + ";请将该异常发送给管理员");
|
|
|
105
|
+ } else {
|
|
|
106
|
+ result.setMsg("系统出现未知异常,请联系管理员!");
|
|
|
107
|
+ }
|
|
|
108
|
+ // 可以尝试着做异常信息持久化
|
|
|
109
|
+ return result;
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+
|
|
|
113
|
+ /**
|
|
|
114
|
+ * 请求出现异常时的响应数据封装
|
|
|
115
|
+ *
|
|
|
116
|
+ * @param customExceptionType 自定义异常枚举中的异常
|
|
|
117
|
+ * @param errorMessage 错误信息
|
|
|
118
|
+ * @return 返回异常信息
|
|
|
119
|
+ */
|
|
|
120
|
+ public static ResultJson error(CustomExceptionType customExceptionType, String errorMessage) {
|
|
|
121
|
+ return new ResultJson<>(customExceptionType.getCode(), errorMessage);
|
|
|
122
|
+ }
|
70
|
} |
123
|
} |