提交 | 用户 | age
|
826b66
|
1 |
package com.hx.resultTool; |
C |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.alibaba.fastjson.JSONArray; |
|
5 |
import com.alibaba.fastjson.JSONObject; |
|
6 |
import com.hx.exception.TipsException; |
|
7 |
|
|
8 |
import java.io.Serializable; |
|
9 |
import java.util.List; |
|
10 |
|
|
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 |
|
|
63 |
/**校验返回码*/ |
|
64 |
public Boolean checkCode(){ |
|
65 |
if(ResponseCode.SUCCESS.equals(code)){ |
|
66 |
return true; |
|
67 |
} |
|
68 |
return false; |
|
69 |
} |
|
70 |
|
|
71 |
/**校验返回码,进行错误提示*/ |
|
72 |
public void checkTips(){ |
|
73 |
if(!ResponseCode.SUCCESS.equals(code)){ |
|
74 |
throw new TipsException("请求失败:"+this.code+","+this.msg); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
/**返回数据转JSONObject*/ |
|
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 |
|
|
88 |
/**返回数据转对象*/ |
|
89 |
public <T> T getObject(Object data, Class<T> clazz) { |
|
90 |
return JSONObject.parseObject(JSON.toJSONString(data), clazz); |
|
91 |
} |
|
92 |
|
|
93 |
/**返回数据转列表*/ |
|
94 |
public <T> List<T> getArray(Object data, Class<T> clazz) { |
|
95 |
return JSONArray.parseArray(JSON.toJSONString(data), clazz); |
|
96 |
} |
|
97 |
|
|
98 |
/*******************************************************************************/ |
|
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 |
|