package util; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; /*** * * @author YangChaoJie * */ public class ReqUtil { /*** * 将请求对象传入,进行对象转Map,排序,进行MD5加密; * * @param req * @return */ private static StringBuilder stringBuilder; private static String currTime; /*** * hashCode加密字符串 * * @return */ public static LinkedHashMap<String, Object> ParameToMap(Object reqData) { stringBuilder = new StringBuilder(); stringBuilder.append("he3yun.com-"); @SuppressWarnings("unchecked") LinkedHashMap<String, Object> map = ZJ_BeanToMapUtils.toMap(reqData); List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Object>>() { public int compare(Entry<String, Object> o1, Entry<String, Object> o2) { return String.valueOf(o1.getValue()).toLowerCase() .compareTo(String.valueOf(o2.getValue()).toLowerCase()); } }); return CheckData(reqData, list); } /*** * 根据不同的构造方法参数生成对应Map对象,进行加密 * * @param reqData * @param list * @return */ private static LinkedHashMap<String, Object> CheckData(Object reqData, List<Map.Entry<String, Object>> list) { LinkedHashMap<String, Object> dataMap = new LinkedHashMap<String, Object>(); return traverseEntryToMap(reqData, dataMap, list); } /*** * 提高复用写的方法 * * @param dataMap * @param list * @return */ private static LinkedHashMap<String, Object> traverseEntryToMap(Object reqData, LinkedHashMap<String, Object> dataMap, List<Map.Entry<String, Object>> list) { for (Entry<String, Object> mapping : list) { if (mapping.getValue() != null) { stringBuilder.append(String.valueOf(mapping.getValue()) + "-"); dataMap.put(mapping.getKey(), mapping.getValue()); } } getCurrTime(); stringBuilder.append(currTime); dataMap.put("hashCode", MD5Util.stringToMD5(String.valueOf(stringBuilder))); dataMap.put("timestamp", currTime); return dataMap; } public static void getCurrTime() { currTime = String.valueOf(new Date().getTime()); } /** * MAP排序 * @param map */ public static LinkedHashMap<String, Object> sortMap(LinkedHashMap<String, Object> map) { List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Object>>() { public int compare(Entry<String, Object> o1, Entry<String, Object> o2) { return String.valueOf(o1.getValue()).toLowerCase() .compareTo(String.valueOf(o2.getValue()).toLowerCase()); } }); return map; } }
反射对象生成没有null的map:
public static <K, V> LinkedHashMap<K, V> toMap(Object javaBean) { LinkedHashMap<K, V> ret = new LinkedHashMap<K, V>(); try { Method[] methods = javaBean.getClass().getDeclaredMethods(); for (Method method : methods) { if (method.getName().startsWith("get")) { String field = method.getName(); field = field.substring(field.indexOf("get") + 3); field = field.toLowerCase().charAt(0) + field.substring(1); Object value = method.invoke(javaBean, (Object[]) null); ret.put((K) field, (V) (null == value ? null : value)); } } } catch (Exception e) { } return ret; }