123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- package com.szwl.model.bo;
- import com.fasterxml.jackson.annotation.JsonProperty;
- import org.apache.commons.beanutils.BeanUtils;
- import org.apache.commons.lang.StringUtils;
- import org.hibernate.collection.AbstractPersistentCollection;
- import java.lang.reflect.Method;
- import java.util.*;
- /**
- * Created by study on 9/2/2015.
- */
- public final class JsonMessage {
- /**
- * 用户未登录
- */
- public static final JsonMessage USER_NOT_LOGIN = JsonMessage.error(2, "用户未登录");
- /**
- * 未知错误
- */
- public static final JsonMessage UNKNOWN_ERROR = JsonMessage.error(4, "未知错误");
- /**
- * 成功代码
- */
- public static final int CODE_SUCCESS = 0;
- /**
- * 基本错误代码
- */
- public static final int CODE_ERROR = 1;
- /**
- * 返回码
- */
- @JsonProperty
- private int code;
- /**
- * 错误信息
- */
- @JsonProperty
- private Object errmsg;
- /**
- * 返回数据
- */
- @JsonProperty
- private Object data;
- // 禁止创建对象
- private JsonMessage(int code, Object data) {
- this.code = code;
- if ( code== 0 ){
- this.data = data;
- } else {
- this.errmsg = data;
- }
- }
- /**
- * /**
- * 成功消息(code=0)<br>
- * 单参数返回格式为:{code:0,data:<Object>}<br>
- * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
- * 返回Json格式{code:0,data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
- *
- * @param data 数据组
- * @return
- */
- public static JsonMessage success(Object... data) {
- return message(CODE_SUCCESS, data);
- }
- /**
- * 失败消息(code=1)<br>
- * 单参数返回格式为:{code:1,data:<Object>}<br>
- * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
- * 返回Json格式{code:1,data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
- *
- * @param data 失败消息
- * @return
- */
- public static JsonMessage error(Object data) {
- return message(CODE_ERROR, data);
- }
- /**
- * 失败消息(code=1)<br>
- * 单参数返回格式为:{code:1,data:<Object>}<br>
- * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
- * 返回Json格式{code:1,data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
- *
- * @param data 失败消息
- * @return
- */
- public static JsonMessage error(int code, Object data) {
- return message(code, data);
- }
- /**
- * 通用消息消息<br>
- * 单参数返回格式为:{code:code(int),data:<Object>}<br>
- * 多参数格式:参数名1,参数值1,参数名2,参数值2...<br>
- * 返回Json格式{code:code(int),data:{'参数1':参数值1(Object),'参数2':参数值2(Object)}}
- *
- * @param code 返回码
- * @param data 返回数据,支持多数据返回,多数据参数格式:参数名1(String),参数值1(Object),参数名2(String),参数值2(Object)...
- * @return
- */
- private static JsonMessage message(int code, Object... data) {
- if (data == null) data = new Object[]{new Object()};
- if (data.length == 1) {
- // 单一数据
- return new JsonMessage(code, data[0]);
- } else {
- Map<String, Object> map = new LinkedHashMap<>();
- for (int i = 0; i < data.length; i++) {
- String key = data[i++].toString();
- map.put(key, data[i]);
- }
- return new JsonMessage(code, map);
- }
- }
- /**
- * 优化对象数据
- *
- * @param bean
- * @param properties
- * @return
- */
- public static Object optimiseObj(Object bean, String... properties) {
- Map<String, Object> map = new HashMap<>();
- if (bean == null) {
- //TODO 前端需要null
- return null;
- // TODO 前端需要空对象
- // return map;
- }
- // 判断是否列表
- if (isCollection(bean.getClass())) {
- Collection list = (Collection) bean;
- return optimiseList(list, properties);
- }
- // 2. 循环每一个属性
- for (String string : properties) {
- if (StringUtils.isEmpty(string)) continue;
- // 处理级联属性
- String[] names = string.split("\\.");
- Map<String, Object> tmp = map;
- Map<String, Object> field;
- // 3. 循环每一个级联属性
- StringBuilder prefix = new StringBuilder();
- for (int i = 0; i < names.length; i++) {
- if (i == (names.length - 1)) {
- // 最后一个field
- tmp.put(names[i], getPropertyValue(bean, string));
- } else {
- field = (Map<String, Object>) tmp.get(names[i]);
- if (field == null) {
- field = new HashMap<>();
- tmp.put(names[i], field);
- }
- prefix.append(names[i]);
- // 处理list
- Class type = getPropertyType(bean, prefix.toString());
- Object obj = getPropertyObject(bean, prefix.toString());
- prefix.append(".");//必须在这里添加
- if (obj == null) {
- tmp.put(names[i], null);
- break;
- } else if (isCollection(type)) {
- String suffix = string.replace(prefix.toString(), "");
- tmp.put(names[i], optimiseList((Collection) getPropertyObject(bean, prefix.substring(0, prefix.length() - 1)), suffix.split("#")));
- break;
- }
- tmp = field; // 必须在处理list之后修改
- }
- }
- }
- return map;
- }
- /**
- * 优化列表数据
- *
- * @param list 列表
- * @param properties 需要的属性
- * @return
- */
- private static Object optimiseList(Collection list, String... properties) {
- List<Object> newList = new ArrayList<>();
- if (list != null && properties != null) {
- // 1. 循环每一个list item
- for (Object bean : list) {
- newList.add(optimiseObj(bean, properties));
- }
- }
- return newList;
- }
- /**
- * 反射获取字段值,支持级联
- *
- * @param bean
- * @param property
- * @return
- */
- private static Object getPropertyValue(Object bean, String property) {
- try {
- Class type = getPropertyType(bean, property);
- if (isCollection(type)) {
- return BeanUtils.getArrayProperty(bean, property);
- } else {
- return BeanUtils.getProperty(bean, property);
- }
- } catch (Exception e) {
- }
- return null;
- }
- /**
- * 获取属性类型,支持级联
- *
- * @param bean
- * @param property
- * @return
- */
- private static Class getPropertyType(Object bean, String property) {
- try {
- String[] names = property.split("\\.");
- Object obj = bean;
- Method method = null;
- for (String name : names) {
- method = obj.getClass().getMethod("get" + toUpperCaseFirstOne(name));
- obj = method.invoke(obj);
- }
- return method.getReturnType();
- } catch (Exception e) {
- }
- return null;
- }
- /**
- * 获取属性对象,支持级联
- *
- * @param bean
- * @param property
- * @return
- */
- private static Object getPropertyObject(Object bean, String property) {
- try {
- String[] names = property.split("\\.");
- Object obj = bean;
- Method method;
- for (String name : names) {
- method = obj.getClass().getMethod("get" + toUpperCaseFirstOne(name));
- obj = method.invoke(obj);
- }
- return obj;
- } catch (Exception e) {
- }
- return null;
- }
- /**
- * 判断类型是否是集合
- *
- * @param type
- * @return
- */
- private static boolean isCollection(Class type) {
- // System.out.println(type);
- while (type != null && !Object.class.equals(type)) {
- Class[] classes = type.getInterfaces();
- for (Class c : classes) {
- if (Collection.class.equals(c)) {
- return true;
- }
- }
- if (AbstractCollection.class.equals(type) || AbstractPersistentCollection.class.equals(type)) {
- return true;
- } else {
- type = type.getSuperclass();
- }
- }
- return false;
- }
- /**
- * 首字母转大写
- *
- * @param s
- * @return
- */
- private static String toUpperCaseFirstOne(String s) {
- if (StringUtils.isEmpty(s)) {
- return "";
- }
- if (Character.isUpperCase(s.charAt(0))) {
- return s;
- } else {
- return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
- }
- }
- }
|