ResponseResultUtil.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.persagy.common.utils;
  2. import com.alibaba.csp.sentinel.util.StringUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.persagy.common.enums.ResponseCode;
  6. /**
  7. * @version
  8. * @description
  9. * @company persagy
  10. * @author zhangqiankun
  11. * @since 2020年9月1日: 下午2:21:25
  12. */
  13. public class ResponseResultUtil {
  14. public static final String RESP_CODE_KEY = "respCode";
  15. public static final String RESP_MSG_KEY = "respMsg";
  16. public static final String RESP_CONTENT_KEY = "content";
  17. public static final String RESP_COUNT_KEY = "count";
  18. public static final String SUCCESS_CODE = ResponseCode.A00000.getCode();
  19. public static final String FAILURE_CODE = ResponseCode.A10000.getCode();
  20. /**
  21. * 判断是否响应成功, respCode=00000
  22. */
  23. public static boolean isSuccess(String resultJson) {
  24. if (StringUtil.isBlank(resultJson)) {
  25. return false;
  26. }
  27. try {
  28. return isSuccess(JSONObject.parseObject(resultJson));
  29. } catch (Exception e) {
  30. }
  31. return false;
  32. }
  33. /**
  34. * 判断是否响应成功, respCode=00000
  35. */
  36. public static boolean isSuccess(JSONObject jsonObject) {
  37. if (jsonObject == null) {
  38. return false;
  39. }
  40. if (SUCCESS_CODE.equals(jsonObject.getString(RESP_CODE_KEY))) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. /**
  46. * 判断是否响应成功, respCode=00000
  47. */
  48. public static boolean isSuccess(ResponseResult result) {
  49. if (result == null) {
  50. return false;
  51. }
  52. if (SUCCESS_CODE.equals(result.getRespCode())) {
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * 判断响应是否失败
  59. */
  60. public static boolean isError(String resultJson) {
  61. return !isSuccess(resultJson);
  62. }
  63. /**
  64. * 判断响应是否失败
  65. */
  66. public static boolean isError(JSONObject jsonObject) {
  67. return !isSuccess(jsonObject);
  68. }
  69. /**
  70. * 判断响应是否失败
  71. */
  72. public static boolean isError(ResponseResult result) {
  73. return !isSuccess(result);
  74. }
  75. /**
  76. * 获取响应体对象
  77. *
  78. * @param resultJson
  79. * @return
  80. */
  81. public static ResponseResult convertResponse(String resultJson) {
  82. if (StringUtil.isBlank(resultJson)) {
  83. return null;
  84. }
  85. try {
  86. return JSONObject.parseObject(resultJson, ResponseResult.class);
  87. } catch (Exception e) {
  88. }
  89. return null;
  90. }
  91. /**
  92. * 获取响应体中content内容所表示的class对象
  93. *
  94. * @param resultJson
  95. * @return
  96. */
  97. public static Class<?> convertResponse(String resultJson, Class<?> clazz) {
  98. JSONObject jsonObject = toJSONObject(resultJson);
  99. return (Class<?>)jsonObject.toJavaObject(clazz);
  100. }
  101. /**
  102. * 获取响应体中content内容所表示的集合对象
  103. *
  104. * @param resultJson
  105. * @return
  106. */
  107. public static Class<?>[] convertResponse(String resultJson, Class<?>[] clazz) {
  108. JSONArray jsonArray = toJSONArray(resultJson);
  109. return (Class<?>[]) jsonArray.toArray(clazz);
  110. }
  111. /**
  112. * 成功响应才会返回content
  113. *
  114. * @param resultJson
  115. * @return 此方法不会返回null
  116. */
  117. public static JSONObject toJSONObject(String resultJson) {
  118. if (StringUtil.isBlank(resultJson)) {
  119. return new JSONObject();
  120. }
  121. JSONObject jsonObject = JSONObject.parseObject(resultJson);
  122. if (isSuccess(jsonObject)) {
  123. return jsonObject.getJSONObject(RESP_CONTENT_KEY);
  124. }
  125. return new JSONObject();
  126. }
  127. /**
  128. * 成功响应才会返回content
  129. *
  130. * @param resultJson
  131. * @return 此方法不会返回null
  132. */
  133. public static JSONArray toJSONArray(String resultJson) {
  134. if (StringUtil.isBlank(resultJson)) {
  135. return new JSONArray();
  136. }
  137. JSONObject jsonObject = JSONObject.parseObject(resultJson);
  138. if (isSuccess(jsonObject)) {
  139. return jsonObject.getJSONArray(RESP_CONTENT_KEY);
  140. }
  141. return new JSONArray();
  142. }
  143. public static ResponseResult successResult() {
  144. ResponseResult result = new ResponseResult();
  145. result.setRespCode(ResponseCode.A00000.getCode());
  146. result.setRespMsg(ResponseCode.A00000.getDesc());
  147. return result;
  148. }
  149. public static ResponseResult successResult(String respMsg) {
  150. ResponseResult result = new ResponseResult();
  151. result.setRespCode(ResponseCode.A00000.getCode());
  152. result.setRespMsg(respMsg);
  153. return result;
  154. }
  155. public static ResponseResult successResult(Object content) {
  156. ResponseResult result = new ResponseResult();
  157. result.setRespCode(ResponseCode.A00000.getCode());
  158. result.setRespMsg(ResponseCode.A00000.getDesc());
  159. result.setContent(content);
  160. return result;
  161. }
  162. public static ResponseResult successResult(String respMsg, Object content) {
  163. ResponseResult result = new ResponseResult();
  164. result.setRespCode(ResponseCode.A00000.getCode());
  165. result.setRespMsg(respMsg);
  166. result.setContent(content);
  167. return result;
  168. }
  169. public static ResponseResult successResult(Object content, Long count) {
  170. ResponseResult result = new ResponseResult();
  171. result.setRespCode(ResponseCode.A00000.getCode());
  172. result.setRespMsg(ResponseCode.A00000.getDesc());
  173. result.setContent(content);
  174. result.setCount(count);
  175. return result;
  176. }
  177. public static ResponseResult errorResult() {
  178. ResponseResult result = new ResponseResult();
  179. result.setRespCode(ResponseCode.A10000.getCode());
  180. result.setRespMsg(ResponseCode.A10000.getDesc());
  181. return result;
  182. }
  183. public static ResponseResult errorResult(String respMsg) {
  184. ResponseResult result = new ResponseResult();
  185. result.setRespCode(ResponseCode.A10000.getCode());
  186. result.setRespMsg(respMsg);
  187. return result;
  188. }
  189. public static ResponseResult errorResult(String respCode, String respMsg) {
  190. ResponseResult result = new ResponseResult();
  191. result.setRespCode(respCode);
  192. result.setRespMsg(respMsg);
  193. return result;
  194. }
  195. public static ResponseResult errorResult(String respCode, String respMsg, Object content) {
  196. ResponseResult result = new ResponseResult();
  197. result.setRespCode(respCode);
  198. result.setRespMsg(respMsg);
  199. result.setContent(content);
  200. return result;
  201. }
  202. }