Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. ```P
  2.  
  3. /**
  4. *
  5. * @Title: LeeJSONResult.java
  6. * @Package com.lee.utils
  7. * @Description: 自定义响应数据结构
  8. * 这个类是提供给门户,ios,安卓,微信商城用的
  9. * 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list)
  10. * 其他自行处理
  11. * 200:表示成功
  12. * 500:表示错误,错误信息在msg字段中
  13. * 501:bean验证错误,不管多少个错误都以map形式返回
  14. * 502:拦截器拦截到用户token出错
  15. * 555:异常抛出信息
  16. * Copyright: Copyright (c) 2016
  17. * Company:Nathan.Lee.Salvatore
  18. *
  19. * @author leechenxiang
  20. * @date 2016年4月22日 下午8:33:36
  21. * @version V1.0
  22. */
  23. public class LeeJSONResult {
  24.  
  25. // 定义jackson对象
  26. private static final ObjectMapper MAPPER = new ObjectMapper();
  27.  
  28. // 响应业务状态
  29. private Integer status;
  30.  
  31. // 响应消息
  32. private String msg;
  33.  
  34. // 响应中的数据
  35. private Object data;
  36.  
  37. private Integer code;
  38.  
  39. private Integer count;
  40.  
  41. public Integer getCode() {
  42. return code;
  43. }
  44.  
  45. public void setCode(Integer code) {
  46. this.code = code;
  47. }
  48.  
  49. public Integer getCount() {
  50. return count;
  51. }
  52.  
  53. public void setCount(Integer count) {
  54. this.count = count;
  55. }
  56.  
  57. private String ok; // 不使用
  58.  
  59. public static LeeJSONResult build(Integer status, String msg, Object data) {
  60. return new LeeJSONResult(status, msg, data);
  61. }
  62.  
  63. public static LeeJSONResult pageBuild(Integer code,Integer count,Object data) {
  64. return new LeeJSONResult(code,count,data);
  65. }
  66.  
  67. public static LeeJSONResult ok(Object data) {
  68. return new LeeJSONResult(data);
  69. }
  70.  
  71. public static LeeJSONResult ok() {
  72. return new LeeJSONResult(null);
  73. }
  74.  
  75. public static LeeJSONResult errorMsg(String msg) {
  76. return new LeeJSONResult(500, msg, null);
  77. }
  78.  
  79. public static LeeJSONResult errorMap(Object data) {
  80. return new LeeJSONResult(501, "error", data);
  81. }
  82.  
  83. public static LeeJSONResult errorTokenMsg(String msg) {
  84. return new LeeJSONResult(502, msg, null);
  85. }
  86.  
  87. public static LeeJSONResult errorException(String msg) {
  88. return new LeeJSONResult(555, msg, null);
  89. }
  90.  
  91. public LeeJSONResult() {
  92.  
  93. }
  94.  
  95. // public static LeeJSONResult build(Integer status, String msg) {
  96. // return new LeeJSONResult(status, msg, null);
  97. // }
  98.  
  99. public LeeJSONResult(Integer status, String msg, Object data) {
  100. this.status = status;
  101. this.msg = msg;
  102. this.data = data;
  103. }
  104.  
  105. public LeeJSONResult(Integer code,Integer count,Object data) {
  106. this.code = code;
  107. this.count = count;
  108. this.data = data;
  109. }
  110.  
  111. public LeeJSONResult(Object data) {
  112. this.status = 200;
  113. this.code = 0;
  114. this.msg = "OK";
  115. this.data = data;
  116. }
  117.  
  118. public Boolean isOK() {
  119. return this.status == 200;
  120. }
  121.  
  122. public Integer getStatus() {
  123. return status;
  124. }
  125.  
  126. public void setStatus(Integer status) {
  127. this.status = status;
  128. }
  129.  
  130. public String getMsg() {
  131. return msg;
  132. }
  133.  
  134. public void setMsg(String msg) {
  135. this.msg = msg;
  136. }
  137.  
  138. public Object getData() {
  139. return data;
  140. }
  141.  
  142. public void setData(Object data) {
  143. this.data = data;
  144. }
  145.  
  146. /**
  147. *
  148. * @Description: 将json结果集转化为LeeJSONResult对象
  149. * 需要转换的对象是一个类
  150. * @param jsonData
  151. * @param clazz
  152. * @return
  153. *
  154. * @author leechenxiang
  155. * @date 2016年4月22日 下午8:34:58
  156. */
  157. public static LeeJSONResult formatToPojo(String jsonData, Class<?> clazz) {
  158. try {
  159. if (clazz == null) {
  160. return MAPPER.readValue(jsonData, LeeJSONResult.class);
  161. }
  162. JsonNode jsonNode = MAPPER.readTree(jsonData);
  163. JsonNode data = jsonNode.get("data");
  164. Object obj = null;
  165. if (clazz != null) {
  166. if (data.isObject()) {
  167. obj = MAPPER.readValue(data.traverse(), clazz);
  168. } else if (data.isTextual()) {
  169. obj = MAPPER.readValue(data.asText(), clazz);
  170. }
  171. }
  172. return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
  173. } catch (Exception e) {
  174. return null;
  175. }
  176. }
  177.  
  178. /**
  179. *
  180. * @Description: 没有object对象的转化
  181. * @param json
  182. * @return
  183. *
  184. * @author leechenxiang
  185. * @date 2016年4月22日 下午8:35:21
  186. */
  187. public static LeeJSONResult format(String json) {
  188. try {
  189. return MAPPER.readValue(json, LeeJSONResult.class);
  190. } catch (Exception e) {
  191. e.printStackTrace();
  192. }
  193. return null;
  194. }
  195.  
  196. /**
  197. *
  198. * @Description: Object是集合转化
  199. * 需要转换的对象是一个list
  200. * @param jsonData
  201. * @param clazz
  202. * @return
  203. *
  204. * @author leechenxiang
  205. * @date 2016年4月22日 下午8:35:31
  206. */
  207. public static LeeJSONResult formatToList(String jsonData, Class<?> clazz) {
  208. try {
  209. JsonNode jsonNode = MAPPER.readTree(jsonData);
  210. JsonNode data = jsonNode.get("data");
  211. Object obj = null;
  212. if (data.isArray() && data.size() > 0) {
  213. obj = MAPPER.readValue(data.traverse(),
  214. MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
  215. }
  216. return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
  217. } catch (Exception e) {
  218. return null;
  219. }
  220. }
  221.  
  222. public String getOk() {
  223. return ok;
  224. }
  225.  
  226. public void setOk(String ok) {
  227. this.ok = ok;
  228. }
  229.  
  230. }
  231.  
  232.  
  233. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement