提交 | 用户 | 时间
|
4d3533
|
1 |
package com.iailab.module.data.common.utils; |
潘 |
2 |
|
|
3 |
import com.iailab.framework.common.enums.ErrorCode; |
|
4 |
|
|
5 |
import java.io.Serializable; |
|
6 |
|
|
7 |
/** |
|
8 |
* 响应数据 |
|
9 |
* |
|
10 |
* @author Mark sunlightcs@gmail.com |
|
11 |
* @since 1.0.0 |
|
12 |
*/ |
|
13 |
public class Result<T> implements Serializable { |
|
14 |
private static final long serialVersionUID = 1L; |
|
15 |
/** |
|
16 |
* 编码:0表示成功,其他值表示失败 |
|
17 |
*/ |
|
18 |
private int code = 0; |
|
19 |
/** |
|
20 |
* 消息内容 |
|
21 |
*/ |
|
22 |
private String msg = "success"; |
|
23 |
/** |
|
24 |
* 响应数据 |
|
25 |
*/ |
|
26 |
private T data; |
|
27 |
|
|
28 |
public Result<T> ok(T data) { |
|
29 |
this.setData(data); |
|
30 |
return this; |
|
31 |
} |
|
32 |
|
|
33 |
public boolean success(){ |
|
34 |
return code == 0; |
|
35 |
} |
|
36 |
|
|
37 |
public Result<T> error() { |
|
38 |
this.code = ErrorCode.INTERNAL_SERVER_ERROR; |
|
39 |
this.msg = MessageUtils.getMessage(this.code); |
|
40 |
return this; |
|
41 |
} |
|
42 |
|
|
43 |
public Result<T> error(int code) { |
|
44 |
this.code = code; |
|
45 |
this.msg = MessageUtils.getMessage(this.code); |
|
46 |
return this; |
|
47 |
} |
|
48 |
|
|
49 |
public Result<T> error(int code, String msg) { |
|
50 |
this.code = code; |
|
51 |
this.msg = msg; |
|
52 |
return this; |
|
53 |
} |
|
54 |
|
|
55 |
public Result<T> error(String msg) { |
|
56 |
this.code = ErrorCode.INTERNAL_SERVER_ERROR; |
|
57 |
this.msg = msg; |
|
58 |
return this; |
|
59 |
} |
|
60 |
|
|
61 |
public int getCode() { |
|
62 |
return code; |
|
63 |
} |
|
64 |
|
|
65 |
public void setCode(int code) { |
|
66 |
this.code = code; |
|
67 |
} |
|
68 |
|
|
69 |
public String getMsg() { |
|
70 |
return msg; |
|
71 |
} |
|
72 |
|
|
73 |
public void setMsg(String msg) { |
|
74 |
this.msg = msg; |
|
75 |
} |
|
76 |
|
|
77 |
public T getData() { |
|
78 |
return data; |
|
79 |
} |
|
80 |
|
|
81 |
public void setData(T data) { |
|
82 |
this.data = data; |
|
83 |
} |
|
84 |
} |