提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.resultTool; |
E |
2 |
|
32e62c
|
3 |
import com.alibaba.fastjson.JSON; |
F |
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.alibaba.fastjson.JSONObject; |
1c6e57
|
6 |
import com.hx.exception.TipsException; |
F |
7 |
|
5c5945
|
8 |
import java.io.Serializable; |
cacc23
|
9 |
import java.util.List; |
5c5945
|
10 |
|
E |
11 |
/** |
|
12 |
* 统一返回格式 |
|
13 |
* @author chenjiahe |
|
14 |
* @Data: 2020-06-20 |
|
15 |
*/ |
|
16 |
public class Result implements Serializable { |
|
17 |
|
|
18 |
private static final long serialVersionUID = -3948389268046368059L; |
|
19 |
|
|
20 |
private String code; |
|
21 |
|
|
22 |
private String msg; |
|
23 |
|
|
24 |
private Object data; |
|
25 |
|
|
26 |
public Result() {} |
|
27 |
|
|
28 |
public Result(String code, String msg) { |
|
29 |
this.code = code; |
|
30 |
this.msg = msg; |
|
31 |
} |
|
32 |
|
|
33 |
public static Result success() { |
|
34 |
Result Result = new Result(); |
|
35 |
Result.setCode(ResponseCode.SUCCESS); |
|
36 |
Result.setMsg("SUCCESS"); |
|
37 |
return Result; |
|
38 |
} |
|
39 |
|
|
40 |
public static Result success(Object data) { |
|
41 |
Result Result = new Result(); |
|
42 |
Result.setCode(ResponseCode.SUCCESS); |
|
43 |
Result.setData(data); |
|
44 |
Result.setMsg("SUCCESS"); |
|
45 |
return Result; |
|
46 |
} |
|
47 |
|
|
48 |
public static Result failure(String code, String msg) { |
|
49 |
Result Result = new Result(); |
|
50 |
Result.setCode(code); |
|
51 |
Result.setMsg(msg); |
|
52 |
return Result; |
|
53 |
} |
|
54 |
|
|
55 |
public static Result failure(String code, String msg, Object data) { |
|
56 |
Result Result = new Result(); |
|
57 |
Result.setCode(code); |
|
58 |
Result.setMsg(msg); |
|
59 |
Result.setData(data); |
|
60 |
return Result; |
|
61 |
} |
|
62 |
|
32668c
|
63 |
/**校验返回码*/ |
C |
64 |
public Boolean checkCode(){ |
c5817f
|
65 |
if(ResponseCode.SUCCESS.equals(code)){ |
C |
66 |
return true; |
32668c
|
67 |
} |
c5817f
|
68 |
return false; |
32668c
|
69 |
} |
C |
70 |
|
1c6e57
|
71 |
/**校验返回码,进行错误提示*/ |
F |
72 |
public void checkTips(){ |
|
73 |
if(!ResponseCode.SUCCESS.equals(code)){ |
|
74 |
throw new TipsException("请求失败:"+this.code+","+this.msg); |
|
75 |
} |
|
76 |
} |
|
77 |
|
32e62c
|
78 |
/**返回数据转JSONObject*/ |
F |
79 |
public JSONObject getJsonObject(Object data){ |
|
80 |
return JSONObject.parseObject(JSON.toJSONString(data)); |
|
81 |
} |
|
82 |
|
|
83 |
/**返回数据转JSONArray*/ |
|
84 |
public JSONArray getJsonArray(Object data){ |
|
85 |
return JSONArray.parseArray(JSON.toJSONString(data)); |
|
86 |
} |
|
87 |
|
bf69a7
|
88 |
/**返回数据转对象*/ |
2d7aee
|
89 |
public <T> T getObject(Object data, Class<T> clazz) { |
W |
90 |
return JSONObject.parseObject(JSON.toJSONString(data), clazz); |
|
91 |
} |
|
92 |
|
bf69a7
|
93 |
/**返回数据转列表*/ |
cacc23
|
94 |
public <T> List<T> getArray(Object data, Class<T> clazz) { |
A |
95 |
return JSONArray.parseArray(JSON.toJSONString(data), clazz); |
bf69a7
|
96 |
} |
A |
97 |
|
5c5945
|
98 |
/*******************************************************************************/ |
E |
99 |
|
|
100 |
public String getCode() { |
|
101 |
return this.code; |
|
102 |
} |
|
103 |
|
|
104 |
public void setCode(final String code) { |
|
105 |
this.code = code; |
|
106 |
} |
|
107 |
|
|
108 |
public String getMsg() { |
|
109 |
return this.msg; |
|
110 |
} |
|
111 |
|
|
112 |
public void setMsg(final String msg) { |
|
113 |
this.msg = msg; |
|
114 |
} |
|
115 |
|
|
116 |
public Object getData() { |
|
117 |
return this.data; |
|
118 |
} |
|
119 |
|
|
120 |
public void setData(final Object data) { |
|
121 |
this.data = data; |
|
122 |
} |
|
123 |
|
|
124 |
} |
|
125 |
|