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