JsonMessage.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package com.szwl.model.bo;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import org.apache.commons.beanutils.BeanUtils;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.hibernate.collection.AbstractPersistentCollection;
  6. import java.lang.reflect.Method;
  7. import java.util.*;
  8. /**
  9. * Created by study on 9/2/2015.
  10. */
  11. public final class JsonMessage {
  12. /**
  13. * 用户未登录
  14. */
  15. public static final JsonMessage USER_NOT_LOGIN = JsonMessage.error(2, "用户未登录");
  16. /**
  17. * 未知错误
  18. */
  19. public static final JsonMessage UNKNOWN_ERROR = JsonMessage.error(4, "未知错误");
  20. /**
  21. * 成功代码
  22. */
  23. public static final int CODE_SUCCESS = 0;
  24. /**
  25. * 基本错误代码
  26. */
  27. public static final int CODE_ERROR = 1;
  28. /**
  29. * 返回码
  30. */
  31. @JsonProperty
  32. private int code;
  33. /**
  34. * 错误信息
  35. */
  36. @JsonProperty
  37. private Object errmsg;
  38. /**
  39. * 返回数据
  40. */
  41. @JsonProperty
  42. private Object data;
  43. // 禁止创建对象
  44. private JsonMessage(int code, Object data) {
  45. this.code = code;
  46. if ( code== 0 ){
  47. this.data = data;
  48. } else {
  49. this.errmsg = data;
  50. }
  51. }
  52. /**
  53. * /**
  54. * 成功消息(code=0)<br>
  55. * 单参数返回格式为:{code:0,data:<Object>}<br>
  56. * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
  57. * 返回Json格式{code:0,data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
  58. *
  59. * @param data 数据组
  60. * @return
  61. */
  62. public static JsonMessage success(Object... data) {
  63. return message(CODE_SUCCESS, data);
  64. }
  65. /**
  66. * 失败消息(code=1)<br>
  67. * 单参数返回格式为:{code:1,data:<Object>}<br>
  68. * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
  69. * 返回Json格式{code:1,data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
  70. *
  71. * @param data 失败消息
  72. * @return
  73. */
  74. public static JsonMessage error(Object data) {
  75. return message(CODE_ERROR, data);
  76. }
  77. /**
  78. * 失败消息(code=1)<br>
  79. * 单参数返回格式为:{code:1,data:<Object>}<br>
  80. * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
  81. * 返回Json格式{code:1,data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
  82. *
  83. * @param data 失败消息
  84. * @return
  85. */
  86. public static JsonMessage error(int code, Object data) {
  87. return message(code, data);
  88. }
  89. /**
  90. * 通用消息消息<br>
  91. * 单参数返回格式为:{code:code(int),data:<Object>}<br>
  92. * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
  93. * 返回Json格式{code:code(int),data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
  94. *
  95. * @param code 返回码
  96. * @param data 返回数据,支持多数据返回,多数据参数格式:参数名1(String),参数值1(Object),参数名2(String),参数值2(Object)...
  97. * @return
  98. */
  99. private static JsonMessage message(int code, Object... data) {
  100. if (data == null) data = new Object[]{new Object()};
  101. if (data.length == 1) {
  102. // 单一数据
  103. return new JsonMessage(code, data[0]);
  104. } else {
  105. Map<String, Object> map = new LinkedHashMap<>();
  106. for (int i = 0; i < data.length; i++) {
  107. String key = data[i++].toString();
  108. map.put(key, data[i]);
  109. }
  110. return new JsonMessage(code, map);
  111. }
  112. }
  113. /**
  114. * 优化对象数据
  115. *
  116. * @param bean
  117. * @param properties
  118. * @return
  119. */
  120. public static Object optimiseObj(Object bean, String... properties) {
  121. Map<String, Object> map = new HashMap<>();
  122. if (bean == null) {
  123. //TODO 前端需要null
  124. return null;
  125. // TODO 前端需要空对象
  126. // return map;
  127. }
  128. // 判断是否列表
  129. if (isCollection(bean.getClass())) {
  130. Collection list = (Collection) bean;
  131. return optimiseList(list, properties);
  132. }
  133. // 2. 循环每一个属性
  134. for (String string : properties) {
  135. if (StringUtils.isEmpty(string)) continue;
  136. // 处理级联属性
  137. String[] names = string.split("\\.");
  138. Map<String, Object> tmp = map;
  139. Map<String, Object> field;
  140. // 3. 循环每一个级联属性
  141. StringBuilder prefix = new StringBuilder();
  142. for (int i = 0; i < names.length; i++) {
  143. if (i == (names.length - 1)) {
  144. // 最后一个field
  145. tmp.put(names[i], getPropertyValue(bean, string));
  146. } else {
  147. field = (Map<String, Object>) tmp.get(names[i]);
  148. if (field == null) {
  149. field = new HashMap<>();
  150. tmp.put(names[i], field);
  151. }
  152. prefix.append(names[i]);
  153. // 处理list
  154. Class type = getPropertyType(bean, prefix.toString());
  155. Object obj = getPropertyObject(bean, prefix.toString());
  156. prefix.append(".");//必须在这里添加
  157. if (obj == null) {
  158. tmp.put(names[i], null);
  159. break;
  160. } else if (isCollection(type)) {
  161. String suffix = string.replace(prefix.toString(), "");
  162. tmp.put(names[i], optimiseList((Collection) getPropertyObject(bean, prefix.substring(0, prefix.length() - 1)), suffix.split("#")));
  163. break;
  164. }
  165. tmp = field; // 必须在处理list之后修改
  166. }
  167. }
  168. }
  169. return map;
  170. }
  171. /**
  172. * 优化列表数据
  173. *
  174. * @param list 列表
  175. * @param properties 需要的属性
  176. * @return
  177. */
  178. private static Object optimiseList(Collection list, String... properties) {
  179. List<Object> newList = new ArrayList<>();
  180. if (list != null && properties != null) {
  181. // 1. 循环每一个list item
  182. for (Object bean : list) {
  183. newList.add(optimiseObj(bean, properties));
  184. }
  185. }
  186. return newList;
  187. }
  188. /**
  189. * 反射获取字段值,支持级联
  190. *
  191. * @param bean
  192. * @param property
  193. * @return
  194. */
  195. private static Object getPropertyValue(Object bean, String property) {
  196. try {
  197. Class type = getPropertyType(bean, property);
  198. if (isCollection(type)) {
  199. return BeanUtils.getArrayProperty(bean, property);
  200. } else {
  201. return BeanUtils.getProperty(bean, property);
  202. }
  203. } catch (Exception e) {
  204. }
  205. return null;
  206. }
  207. /**
  208. * 获取属性类型,支持级联
  209. *
  210. * @param bean
  211. * @param property
  212. * @return
  213. */
  214. private static Class getPropertyType(Object bean, String property) {
  215. try {
  216. String[] names = property.split("\\.");
  217. Object obj = bean;
  218. Method method = null;
  219. for (String name : names) {
  220. method = obj.getClass().getMethod("get" + toUpperCaseFirstOne(name));
  221. obj = method.invoke(obj);
  222. }
  223. return method.getReturnType();
  224. } catch (Exception e) {
  225. }
  226. return null;
  227. }
  228. /**
  229. * 获取属性对象,支持级联
  230. *
  231. * @param bean
  232. * @param property
  233. * @return
  234. */
  235. private static Object getPropertyObject(Object bean, String property) {
  236. try {
  237. String[] names = property.split("\\.");
  238. Object obj = bean;
  239. Method method;
  240. for (String name : names) {
  241. method = obj.getClass().getMethod("get" + toUpperCaseFirstOne(name));
  242. obj = method.invoke(obj);
  243. }
  244. return obj;
  245. } catch (Exception e) {
  246. }
  247. return null;
  248. }
  249. /**
  250. * 判断类型是否是集合
  251. *
  252. * @param type
  253. * @return
  254. */
  255. private static boolean isCollection(Class type) {
  256. // System.out.println(type);
  257. while (type != null && !Object.class.equals(type)) {
  258. Class[] classes = type.getInterfaces();
  259. for (Class c : classes) {
  260. if (Collection.class.equals(c)) {
  261. return true;
  262. }
  263. }
  264. if (AbstractCollection.class.equals(type) || AbstractPersistentCollection.class.equals(type)) {
  265. return true;
  266. } else {
  267. type = type.getSuperclass();
  268. }
  269. }
  270. return false;
  271. }
  272. /**
  273. * 首字母转大写
  274. *
  275. * @param s
  276. * @return
  277. */
  278. private static String toUpperCaseFirstOne(String s) {
  279. if (StringUtils.isEmpty(s)) {
  280. return "";
  281. }
  282. if (Character.isUpperCase(s.charAt(0))) {
  283. return s;
  284. } else {
  285. return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
  286. }
  287. }
  288. }