RestTemplate當APPLICATION_FORM_URLENCODED類型時不支持HashMap,處理辦法

使用MultiValueMapapp

下列是把對象轉換成Map HttpEntity<?> requestEntity = ( mediaType == MediaType.APPLICATION_JSON || mediaType == MediaType.APPLICATION_JSON_UTF8) ? new HttpEntity<JSONObject>(params, requestHeaders) : (mediaType == MediaType.APPLICATION_FORM_URLENCODED ? new HttpEntity<MultiValueMap>(createMultiValueMap(params), requestHeaders) : new HttpEntity<>(null, requestHeaders));ui

createMultiValueMap private static String expandURL(String url, Set<?> keys) { final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?"); Matcher mc = QUERY_PARAM_PATTERN.matcher(url); StringBuilder sb = new StringBuilder(url); if (mc.find()) { sb.append("&"); } else { sb.append("?"); }url

for (Object key : keys) {
        sb.append(key).append("=").append("{").append(key).append("}").append("&");
    }
    return sb.deleteCharAt(sb.length() - 1).toString();
}
相關文章
相關標籤/搜索