PageRenderTime 114ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/SHJS/src/main/java/com/sunwave/framework/util/JsonResponse.java

https://gitlab.com/dannyblue/danny-project
Java | 334 lines | 143 code | 28 blank | 163 comment | 0 complexity | a2a985a3ff893cc6adb897ae07295b26 MD5 | raw file
  1. package com.sunwave.framework.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.serializer.SerializerFeature;
  5. import com.google.common.collect.ImmutableMap;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * <p>
  12. * JsonResponse class.
  13. * </p>
  14. *
  15. * @author
  16. * @version
  17. */
  18. public final class JsonResponse {
  19. private JsonResponse() {
  20. }
  21. private static SerializerFeature[] features = {SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect};
  22. /**
  23. * <p>
  24. * badResult.
  25. * </p>
  26. *
  27. * @param cause a {@link String} object.
  28. * @return a {@link String} object.
  29. */
  30. public static String badResult(String cause) {
  31. JSONObject result = new JSONObject();
  32. result.put("errorCode", 1);
  33. result.put("errorMessage", cause);
  34. return JSON.toJSONString(result, features);
  35. }
  36. /**
  37. * <p>
  38. * emptyResult.
  39. * </p>
  40. *
  41. * @param cause a {@link String} object.
  42. * @return a {@link String} object.
  43. */
  44. public static String emptyResult(String cause) {
  45. JSONObject result = new JSONObject();
  46. result.put("errorCode", 2);
  47. result.put("errorMessage", cause);
  48. return JSON.toJSONString(result, features);
  49. }
  50. /**
  51. * jsonp请求失败
  52. *
  53. * @param cause a {@link String} object.
  54. * @param callback a {@link String} object.
  55. * @return a {@link String} object.
  56. */
  57. public static String badResult(String cause, String callback) {
  58. JSONObject result = new JSONObject();
  59. result.put("errorCode", 1);
  60. result.put("errorMessage", cause);
  61. return callback + "(" + JSON.toJSONString(result, features) + ")";
  62. }
  63. /**
  64. * <p>
  65. * badResult.
  66. * </p>
  67. *
  68. * @param cause a {@link java.util.Map} object.
  69. * @return a {@link String} object.
  70. */
  71. public static String badResult(Map<String, String> cause) {
  72. JSONObject result = new JSONObject();
  73. result.put("errorCode", 1);
  74. result.put("errorMessage", cause);
  75. return JSON.toJSONString(result, features);
  76. }
  77. /**
  78. * 区分错误类型
  79. *
  80. * @param type a int.
  81. * @param cause a {@link String} object.
  82. * @return a {@link String} object.
  83. */
  84. public static String badResult(int type, String cause) {
  85. JSONObject result = new JSONObject();
  86. result.put("errorCode", 1);
  87. result.put("errorMessage", ImmutableMap.of("type", type, "msg", cause));
  88. return JSON.toJSONString(result, features);
  89. }
  90. /**
  91. * <p>
  92. * ok.
  93. * </p>
  94. *
  95. * @return a {@link String} object.
  96. */
  97. public static String ok() {
  98. JSONObject result = new JSONObject();
  99. result.put("errorCode", 0);
  100. result.put("data", "成功");
  101. return result.toString();
  102. }
  103. /**
  104. * <p>
  105. * okWithEmpty.
  106. * </p>
  107. *
  108. * @return a {@link String} object.
  109. */
  110. public static String okWithEmpty() {
  111. JSONObject result = new JSONObject();
  112. result.put("errorCode", 0);
  113. result.put("data", Collections.emptyList());
  114. return result.toString();
  115. }
  116. /**
  117. * <p>
  118. * ok.
  119. * </p>
  120. *
  121. * @param key a {@link String} object.
  122. * @param value a {@link Object} object.
  123. * @return a {@link String} object.
  124. */
  125. public static String ok(String key, Object value) {
  126. JSONObject result = new JSONObject();
  127. result.put("errorCode", 0);
  128. result.put("data", ImmutableMap.of(key, value));
  129. return JSON.toJSONString(result, features);
  130. }
  131. /**
  132. * <p>
  133. * ok.
  134. * </p>
  135. *
  136. * @param object a {@link Object} object.
  137. * @return a {@link String} object.
  138. */
  139. public static Object ok(Object object) {
  140. JSONObject result = new JSONObject();
  141. result.put("errorCode", 0);
  142. result.put("data", object);
  143. String jsonString = JSON.toJSONString(result, features);
  144. return JSON.parse(jsonString);
  145. }
  146. /**
  147. * 返回分页结果json字串
  148. *
  149. * @param object 数据对象
  150. * @param pageTotal 结果列表总共多少页
  151. * @param pageSize 每页多少条记录
  152. * @param pageNo 页号
  153. * @return json 字串
  154. */
  155. public static String okWithPaginate(Object object, int pageTotal, int pageSize, int pageNo) {
  156. JSONObject result = new JSONObject();
  157. result.put("totalCount", pageTotal);
  158. result.put("pageSize", pageSize);
  159. result.put("pageNo", pageNo);
  160. result.put("data", object);
  161. result.put("errorCode", 0);
  162. return JSON.toJSONString(result, features);
  163. }
  164. /**
  165. * 返回分页结果json字串
  166. *
  167. * @param object 数据对象
  168. * @param pageTotal 结果列表总共多少页
  169. * @param pageSize 每页多少条记录
  170. * @param pageNo 页号
  171. * @param count 数据条数
  172. * @return json 对象
  173. */
  174. public static JSONObject okWithPaginate(Object object, int count) {
  175. JSONObject result = new JSONObject();
  176. result.put("data", object);
  177. result.put("totalCount", count);
  178. result.put("errorCode", 0);
  179. return result;
  180. }
  181. /**
  182. * <p>
  183. * jsonp.
  184. * </p>
  185. *
  186. * @param object a {@link Object} object.
  187. * @param callback a {@link String} object.
  188. * @return a {@link String} object.
  189. */
  190. public static String jsonp(Object object, String callback) {
  191. JSONObject result = new JSONObject();
  192. result.put("errorCode", 0);
  193. result.put("data", object);
  194. return callback + "(" + JSON.toJSONString(result, features) + ")";
  195. }
  196. /**
  197. * 可以通过map形式传递参数,但是如果是ok,msg这两个参数的值会被覆盖掉
  198. *
  199. * @param params a {@link java.util.Map} object.
  200. * @return a {@link String} object.
  201. */
  202. public static Object ok(Map<String, Object> params) {
  203. JSONObject result = new JSONObject();
  204. result.put("errorCode", 0);
  205. result.put("data", params);
  206. return JSON.parse(JSON.toJSONString(result, features));
  207. }
  208. /**
  209. * 可以通过map形式传递参数,但是如果是ok,msg这两个参数的值会被覆盖掉
  210. *
  211. * @param params a {@link com.google.common.collect.ImmutableMap} object.
  212. * @return a {@link String} object.
  213. */
  214. public static String ok(ImmutableMap<String, Object> params) {
  215. JSONObject result = new JSONObject();
  216. result.put("errorCode", 0);
  217. result.put("data", params);
  218. return JSON.toJSONString(result, features);
  219. }
  220. /**
  221. * 返回分页结果json字串
  222. *
  223. * @param list
  224. * @param pageTotal
  225. * @param pageSize
  226. * @param pageNo
  227. * @param <T>
  228. * @return json 字串
  229. */
  230. public static <T> String okWithPaginate(List<T> list, int pageTotal, int pageSize, int pageNo) {
  231. JSONObject result = new JSONObject();
  232. result.put("totalCount", pageTotal);
  233. result.put("pageSize", pageSize);
  234. result.put("pageNo", pageNo);
  235. result.put("errorCode", 0);
  236. result.put("data", list);
  237. return JSON.toJSONString(result, features);
  238. }
  239. /**
  240. * 返回分页结果的json
  241. *
  242. * @param params
  243. * @param pageTotal
  244. * @param pageSize
  245. * @param pageNo
  246. * @return json 字串
  247. */
  248. public static String okWithPaginate(Map<String, Object> params, int pageTotal, int pageSize, int pageNo) {
  249. JSONObject result = new JSONObject();
  250. result.put("totalCount", pageTotal);
  251. result.put("pageSize", pageSize);
  252. result.put("pageNo", pageNo);
  253. result.put("errorCode", 0);
  254. result.put("data", params);
  255. return JSON.toJSONString(result, features);
  256. }
  257. /**
  258. * 返回分页结果的json
  259. *
  260. * @param params
  261. * @param pageTotal
  262. * @param pageSize
  263. * @param pageNo
  264. * @return json 字串
  265. */
  266. public static String okWithPaginateDisableRef(Map<String, Object> params, int pageTotal, int pageSize, int pageNo) {
  267. JSONObject result = new JSONObject();
  268. result.put("totalCount", pageTotal);
  269. result.put("pageSize", pageSize);
  270. result.put("pageNo", pageNo);
  271. result.put("errorCode", 0);
  272. result.put("data", params);
  273. return JSON.toJSONString(result, features);
  274. }
  275. /**
  276. * 返回分页结果json字串,去掉fastjson循环引用功能
  277. *
  278. * @param object 数据对象
  279. * @param pageTotal 结果列表总共多少页
  280. * @param pageSize 每页多少条记录
  281. * @param pageNo 页号
  282. * @param count 数据条数
  283. * @return json 字串
  284. */
  285. public static Object okWithPaginateDisableRef(Object object, int count) {
  286. JSONObject result = new JSONObject();
  287. result.put("data", object);
  288. result.put("totalCount", count);
  289. result.put("errorCode", 0);
  290. //消除fastjson对同一对象循环引用的问题
  291. String jsonString = JSON.toJSONString(result, features);
  292. return JSON.parse(jsonString);
  293. }
  294. /**
  295. * <p>
  296. * main.
  297. * </p>
  298. *
  299. * @param args an array of {@link String} objects.
  300. */
  301. public static void main(String[] args) {
  302. Map<String, Object> params = new HashMap<String, Object>();
  303. params.put("test", 1);
  304. params.put("test1", "ok");
  305. }
  306. }