CommonResult.java
3.98 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.hotent.entity;
import com.hotent.enums.ResponseErrorEnums;
import java.io.Serializable;
public class CommonResult<E> implements Serializable {
private static final long serialVersionUID = 1L;
Boolean state = true;
String message;
E value;
private Integer code = 200;
private String errorCode;
private String logId;
/**
* 构造成功的返回结果
* <p>
* 例如:CommonResult.ok(); 或者 CommonResult.<String>ok();<br />
* 结果集中各属性默认值如下:<br />
* state: true<br />
* message: "操作成功"<br />
* code: 200
* </p>
* @param <E>
* @return
*/
public static <E> CommonResult<E> ok() {
CommonResult<E> cr = new CommonResult<>(ResponseErrorEnums.SUCCESS_OPTION);
return cr;
}
/**
* 构造成功的返回结果
* <p>
* 例如:CommonResult.error(); 或者 CommonResult.<String>error();<br />
* 结果集中各属性默认值如下:<br />
* state: false<br />
* message: "系统异常"<br />
* code: 500
* </p>
* @param <E>
* @return
*/
public static <E> CommonResult<E> error() {
CommonResult<E> cr = new CommonResult<>(ResponseErrorEnums.SYSTEM_ERROR);
return cr;
}
/**
* 构造指定的返回结果
* <p>
* 例如:CommonResult.result(ResponseErrorEnums.SUCCESS_OPTION);<br />
* 结果集中各属性对应传入的ResponseErrorEnums
* </p>
* @param <E>
* @return
*/
public static <E> CommonResult<E> result(ResponseErrorEnums res) {
CommonResult<E> cr = new CommonResult<>(res);
return cr;
}
/**
* 设置message并返回结果
* @param message
* @return
*/
public CommonResult<E> message(String message){
this.setMessage(message);
return this;
}
/**
* 设置value并返回结果
* @param val
* @return
*/
public CommonResult<E> value(E val){
this.setValue(val);
return this;
}
/**
* 设置logId并返回结果
* @param logId
* @return
*/
public CommonResult<E> log(String logId){
this.setLogId(logId);
return this;
}
public CommonResult() {
}
/**
* 返回成功及成功的提示信息
* @param message
*/
public CommonResult(String message) {
this(true, message, null);
}
/**
* 返回成功/失败,及对应的成功/失败提示信息
* @param state
* @param message
*/
public CommonResult(boolean state, String message) {
this(state, message, null);
}
/**
* 返回成功/失败,及对应的成功/失败提示信息,还有返回对应的数据
* @param state
* @param message
* @param value
*/
public CommonResult(boolean state, String message, E value){
this.state = state;
this.message = message;
this.value = value;
}
/**
* 返回错误,及错误编码,对应的错误信息
* @param error
*/
public CommonResult(ResponseErrorEnums error) {
if(!error.getCode().equals("200")){
this.state = false;
}
this.errorCode = error.getCode();
this.message = error.getMessage();
this.code = error.getHttpCode();
}
/**
* 返回错误,及错误编码,对应的错误信息,还有返回对应的数据
* @param error
* @param value
*/
public CommonResult(ResponseErrorEnums error, E value) {
if(!error.getCode().equals("200")){
this.state = false;
}
this.errorCode = error.getCode();
this.message = error.getMessage();
this.code = error.getHttpCode();
this.value = value;
}
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public String getLogId() {
return logId;
}
public void setLogId(String logId) {
this.logId = logId;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}