1. 程式人生 > 其它 >按照引數名稱的字典順序對請求中所有的請求引數(包括公共請求引數和介面的自定義引數,但不包括公共請求引數中的Signature引數)進行排序

按照引數名稱的字典順序對請求中所有的請求引數(包括公共請求引數和介面的自定義引數,但不包括公共請求引數中的Signature引數)進行排序

引數進行字典順序排序;

呼叫阿里雲訊息佇列RocketMQ版http呼叫方式請求介面所需

    /**
     * @param paraMap   請求引數
     * @param urlEncode  是否utf-8編碼
     * @return
     */
    public static String formatUrlMap(Map<String, String> paraMap, boolean urlEncode)
    {
        String buff = "";
        Map<String, String> tmpMap = paraMap;
        
try { List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(tmpMap.entrySet()); // 對所有傳入引數按照欄位名的 ASCII 碼從小到大排序(字典序) Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() { @Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { return (o1.getKey()).toString().compareTo(o2.getKey()); } }); // 構造返回字串鍵值對的格式 StringBuilder buf = new StringBuilder();
for (Map.Entry<String, String> item : infoIds) { if (StringUtils.isNotBlank(item.getKey())) { String key = item.getKey(); String val = item.getValue(); if (urlEncode) { val = URLEncoder.encode(val, "utf-8"); } buf.append(key + "=" + val); buf.append("&"); } } buff = buf.toString(); if (buff.isEmpty() == false) { buff = buff.substring(0, buff.length() - 1); } } catch (Exception e) { return null; } return buff; }