|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Map;
|
|
@@ -173,6 +174,8 @@ public class RestUtil {
|
|
|
if (StringUtils.isEmpty(url)) {
|
|
|
throw new RuntimeException("url 不能为空");
|
|
|
}
|
|
|
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
|
+
|
|
|
if (method == null) {
|
|
|
throw new RuntimeException("method 不能为空");
|
|
|
}
|
|
@@ -186,11 +189,12 @@ public class RestUtil {
|
|
|
}
|
|
|
|
|
|
if (variables != null) {
|
|
|
- url += ("?" + asUrlVariables(variables));
|
|
|
+
|
|
|
+ asUrlVariables(builder, variables);
|
|
|
}
|
|
|
|
|
|
HttpEntity<String> request = new HttpEntity<>(body, headers);
|
|
|
- return RT.exchange(url, method, request, responseType);
|
|
|
+ return RT.exchange(builder.encode().build().toUri(), method, request, responseType);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -213,7 +217,7 @@ public class RestUtil {
|
|
|
|
|
|
* 将 JSONObject 转为 a=1&b=2&c=3...&n=n 的形式
|
|
|
*/
|
|
|
- public static String asUrlVariables(JSONObject variables) {
|
|
|
+ public static String asUrlVariables(UriComponentsBuilder builder, JSONObject variables) {
|
|
|
Map<String, Object> source = variables.getInnerMap();
|
|
|
Iterator<String> it = source.keySet().iterator();
|
|
|
StringBuilder urlVariables = new StringBuilder();
|
|
@@ -226,10 +230,12 @@ public class RestUtil {
|
|
|
value = object.toString();
|
|
|
}
|
|
|
}
|
|
|
- urlVariables.append("&").append(key).append("=").append(value);
|
|
|
+ builder.queryParam(key, value);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- return urlVariables.substring(1);
|
|
|
+
|
|
|
+ return "";
|
|
|
}
|
|
|
|
|
|
}
|