提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.common.pojo; |
H |
2 |
|
d66edb
|
3 |
import cn.hutool.core.lang.Assert; |
e7c126
|
4 |
import com.iailab.framework.common.exception.ErrorCode; |
H |
5 |
import com.iailab.framework.common.exception.ServiceException; |
|
6 |
import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; |
|
7 |
import com.fasterxml.jackson.annotation.JsonIgnore; |
d66edb
|
8 |
import com.iailab.framework.common.exception.util.ServiceExceptionUtil; |
e7c126
|
9 |
import lombok.Data; |
H |
10 |
|
|
11 |
import java.io.Serializable; |
|
12 |
import java.util.Objects; |
|
13 |
|
|
14 |
/** |
|
15 |
* 通用返回 |
|
16 |
* |
|
17 |
* @param <T> 数据泛型 |
|
18 |
*/ |
|
19 |
@Data |
|
20 |
public class CommonResult<T> implements Serializable { |
|
21 |
|
|
22 |
/** |
|
23 |
* 错误码 |
|
24 |
* |
|
25 |
* @see ErrorCode#getCode() |
|
26 |
*/ |
|
27 |
private Integer code; |
|
28 |
/** |
|
29 |
* 返回数据 |
|
30 |
*/ |
|
31 |
private T data; |
|
32 |
/** |
|
33 |
* 错误提示,用户可阅读 |
|
34 |
* |
|
35 |
* @see ErrorCode#getMsg() () |
|
36 |
*/ |
|
37 |
private String msg; |
|
38 |
|
|
39 |
/** |
|
40 |
* 将传入的 result 对象,转换成另外一个泛型结果的对象 |
|
41 |
* |
|
42 |
* 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。 |
|
43 |
* |
|
44 |
* @param result 传入的 result 对象 |
d66edb
|
45 |
* @param <T> 返回的泛型 |
e7c126
|
46 |
* @return 新的 CommonResult 对象 |
H |
47 |
*/ |
|
48 |
public static <T> CommonResult<T> error(CommonResult<?> result) { |
|
49 |
return error(result.getCode(), result.getMsg()); |
|
50 |
} |
|
51 |
|
|
52 |
public static <T> CommonResult<T> error(Integer code, String message) { |
d66edb
|
53 |
cn.hutool.core.lang.Assert.notEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), code, "code 必须是错误的!"); |
e7c126
|
54 |
CommonResult<T> result = new CommonResult<>(); |
H |
55 |
result.code = code; |
|
56 |
result.msg = message; |
d66edb
|
57 |
return result; |
H |
58 |
} |
|
59 |
|
|
60 |
public static <T> CommonResult<T> error(ErrorCode errorCode, Object... params) { |
|
61 |
Assert.notEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), errorCode.getCode(), "code 必须是错误的!"); |
|
62 |
CommonResult<T> result = new CommonResult<>(); |
|
63 |
result.code = errorCode.getCode(); |
|
64 |
result.msg = ServiceExceptionUtil.doFormat(errorCode.getCode(), errorCode.getMsg(), params); |
e7c126
|
65 |
return result; |
H |
66 |
} |
|
67 |
|
|
68 |
public static <T> CommonResult<T> error(ErrorCode errorCode) { |
|
69 |
return error(errorCode.getCode(), errorCode.getMsg()); |
|
70 |
} |
|
71 |
|
|
72 |
public static <T> CommonResult<T> success(T data) { |
|
73 |
CommonResult<T> result = new CommonResult<>(); |
|
74 |
result.code = GlobalErrorCodeConstants.SUCCESS.getCode(); |
|
75 |
result.data = data; |
|
76 |
result.msg = ""; |
449017
|
77 |
return result; |
D |
78 |
} |
|
79 |
|
fab7c0
|
80 |
public static CommonResult<String> success() { |
潘 |
81 |
CommonResult<String> result = new CommonResult<>(); |
|
82 |
result.code = GlobalErrorCodeConstants.SUCCESS.getCode(); |
|
83 |
result.msg = "success"; |
|
84 |
return result; |
|
85 |
} |
|
86 |
|
e7c126
|
87 |
public static boolean isSuccess(Integer code) { |
H |
88 |
return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode()); |
|
89 |
} |
|
90 |
|
|
91 |
@JsonIgnore // 避免 jackson 序列化 |
|
92 |
public boolean isSuccess() { |
|
93 |
return isSuccess(code); |
|
94 |
} |
|
95 |
|
|
96 |
@JsonIgnore // 避免 jackson 序列化 |
|
97 |
public boolean isError() { |
|
98 |
return !isSuccess(); |
|
99 |
} |
|
100 |
|
|
101 |
// ========= 和 Exception 异常体系集成 ========= |
|
102 |
|
|
103 |
/** |
|
104 |
* 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常 |
|
105 |
*/ |
|
106 |
public void checkError() throws ServiceException { |
|
107 |
if (isSuccess()) { |
|
108 |
return; |
|
109 |
} |
|
110 |
// 业务异常 |
|
111 |
throw new ServiceException(code, msg); |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常 |
|
116 |
* 如果没有,则返回 {@link #data} 数据 |
|
117 |
*/ |
|
118 |
@JsonIgnore // 避免 jackson 序列化 |
|
119 |
public T getCheckedData() { |
|
120 |
checkError(); |
|
121 |
return data; |
|
122 |
} |
|
123 |
|
|
124 |
public static <T> CommonResult<T> error(ServiceException serviceException) { |
|
125 |
return error(serviceException.getCode(), serviceException.getMessage()); |
|
126 |
} |
|
127 |
|
|
128 |
} |