提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.common.core.domain; |
懒 |
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
import com.jcdm.common.constant.HttpStatus; |
|
5 |
|
|
6 |
/** |
|
7 |
* 响应信息主体 |
|
8 |
* |
|
9 |
* @author jc |
|
10 |
*/ |
|
11 |
public class R<T> implements Serializable |
|
12 |
{ |
|
13 |
private static final long serialVersionUID = 1L; |
|
14 |
|
|
15 |
/** 成功 */ |
|
16 |
public static final int SUCCESS = HttpStatus.SUCCESS; |
|
17 |
|
|
18 |
/** 失败 */ |
|
19 |
public static final int FAIL = HttpStatus.ERROR; |
|
20 |
|
|
21 |
private int code; |
|
22 |
|
|
23 |
private String msg; |
|
24 |
|
|
25 |
private T data; |
|
26 |
|
|
27 |
public static <T> R<T> ok() |
|
28 |
{ |
|
29 |
return restResult(null, SUCCESS, "操作成功"); |
|
30 |
} |
|
31 |
|
|
32 |
public static <T> R<T> ok(T data) |
|
33 |
{ |
|
34 |
return restResult(data, SUCCESS, "操作成功"); |
|
35 |
} |
|
36 |
|
|
37 |
public static <T> R<T> ok(T data, String msg) |
|
38 |
{ |
|
39 |
return restResult(data, SUCCESS, msg); |
|
40 |
} |
|
41 |
|
|
42 |
public static <T> R<T> fail() |
|
43 |
{ |
|
44 |
return restResult(null, FAIL, "操作失败"); |
|
45 |
} |
|
46 |
|
|
47 |
public static <T> R<T> fail(String msg) |
|
48 |
{ |
|
49 |
return restResult(null, FAIL, msg); |
|
50 |
} |
|
51 |
|
|
52 |
public static <T> R<T> fail(T data) |
|
53 |
{ |
|
54 |
return restResult(data, FAIL, "操作失败"); |
|
55 |
} |
|
56 |
|
|
57 |
public static <T> R<T> fail(T data, String msg) |
|
58 |
{ |
|
59 |
return restResult(data, FAIL, msg); |
|
60 |
} |
|
61 |
|
|
62 |
public static <T> R<T> fail(int code, String msg) |
|
63 |
{ |
|
64 |
return restResult(null, code, msg); |
|
65 |
} |
|
66 |
|
|
67 |
private static <T> R<T> restResult(T data, int code, String msg) |
|
68 |
{ |
|
69 |
R<T> apiResult = new R<>(); |
|
70 |
apiResult.setCode(code); |
|
71 |
apiResult.setData(data); |
|
72 |
apiResult.setMsg(msg); |
|
73 |
return apiResult; |
|
74 |
} |
|
75 |
|
|
76 |
public int getCode() |
|
77 |
{ |
|
78 |
return code; |
|
79 |
} |
|
80 |
|
|
81 |
public void setCode(int code) |
|
82 |
{ |
|
83 |
this.code = code; |
|
84 |
} |
|
85 |
|
|
86 |
public String getMsg() |
|
87 |
{ |
|
88 |
return msg; |
|
89 |
} |
|
90 |
|
|
91 |
public void setMsg(String msg) |
|
92 |
{ |
|
93 |
this.msg = msg; |
|
94 |
} |
|
95 |
|
|
96 |
public T getData() |
|
97 |
{ |
|
98 |
return data; |
|
99 |
} |
|
100 |
|
|
101 |
public void setData(T data) |
|
102 |
{ |
|
103 |
this.data = data; |
|
104 |
} |
|
105 |
|
|
106 |
public static <T> Boolean isError(R<T> ret) |
|
107 |
{ |
|
108 |
return !isSuccess(ret); |
|
109 |
} |
|
110 |
|
|
111 |
public static <T> Boolean isSuccess(R<T> ret) |
|
112 |
{ |
|
113 |
return R.SUCCESS == ret.getCode(); |
|
114 |
} |
|
115 |
} |