RestUtil.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package com.sooka.sponest.songhuahu.util;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.http.*;
  6. import org.springframework.http.client.SimpleClientHttpRequestFactory;
  7. import org.springframework.http.converter.StringHttpMessageConverter;
  8. import org.springframework.web.client.RestTemplate;
  9. import java.nio.charset.StandardCharsets;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. /**
  13. * 调用 Restful 接口 Util
  14. *
  15. * @author bihs
  16. */
  17. @Slf4j
  18. public class RestUtil {
  19. /**
  20. * RestAPI 调用器
  21. */
  22. private final static RestTemplate RT;
  23. static {
  24. SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
  25. requestFactory.setConnectTimeout(300000);
  26. requestFactory.setReadTimeout(300000);
  27. RT = new RestTemplate(requestFactory);
  28. // 解决乱码问题
  29. RT.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
  30. }
  31. public static RestTemplate getRestTemplate() {
  32. return RT;
  33. }
  34. /**
  35. * 发送 get 请求
  36. */
  37. public static JSONObject get(String url) {
  38. return getNative(url, null, null).getBody();
  39. }
  40. /**
  41. * 发送 get 请求
  42. */
  43. public static JSONObject get(String url, JSONObject variables) {
  44. return getNative(url, variables, null).getBody();
  45. }
  46. /**
  47. * 发送 get 请求
  48. */
  49. public static JSONObject get(String url, JSONObject variables, JSONObject params) {
  50. return getNative(url, variables, params).getBody();
  51. }
  52. /**
  53. * 发送 get 请求,返回原生 ResponseEntity 对象
  54. */
  55. public static ResponseEntity<JSONObject> getNative(String url, JSONObject variables, JSONObject params) {
  56. return request(url, HttpMethod.GET, variables, params);
  57. }
  58. /**
  59. * 发送 Post 请求
  60. */
  61. public static JSONObject post(String url) {
  62. return postNative(url, null, new JSONObject()).getBody();
  63. }
  64. /**
  65. * 发送 Post 请求
  66. */
  67. public static JSONObject post(String url, JSONObject params) {
  68. return postNative(url, null, params).getBody();
  69. }
  70. /**
  71. * 发送 Post 请求
  72. */
  73. public static JSONObject post(String url, JSONArray params) {
  74. return postNative(url, null, params).getBody();
  75. }
  76. /**
  77. * 发送 Post 请求
  78. */
  79. public static JSONObject post(String url, JSONObject variables, JSONObject params) {
  80. return postNative(url, variables, params).getBody();
  81. }
  82. /**
  83. * 发送 Post 请求
  84. */
  85. public static JSONObject post(String url, JSONObject variables, JSONArray params) {
  86. return postNative(url, variables, params).getBody();
  87. }
  88. /**
  89. * 发送 POST 请求,返回原生 ResponseEntity 对象
  90. */
  91. public static ResponseEntity<JSONObject> postNative(String url, JSONObject variables, JSONObject params) {
  92. return request(url, HttpMethod.POST, variables, params);
  93. }
  94. /**
  95. * 发送 POST 请求,返回原生 ResponseEntity 对象
  96. */
  97. public static ResponseEntity<JSONObject> postNative(String url, JSONObject variables, JSONArray params) {
  98. return request(url, HttpMethod.POST, variables, params);
  99. }
  100. /**
  101. * 发送 put 请求
  102. */
  103. public static JSONObject put(String url) {
  104. return putNative(url, null, null).getBody();
  105. }
  106. /**
  107. * 发送 put 请求
  108. */
  109. public static JSONObject put(String url, JSONObject params) {
  110. return putNative(url, null, params).getBody();
  111. }
  112. /**
  113. * 发送 put 请求
  114. */
  115. public static JSONObject put(String url, JSONObject variables, JSONObject params) {
  116. return putNative(url, variables, params).getBody();
  117. }
  118. /**
  119. * 发送 put 请求,返回原生 ResponseEntity 对象
  120. */
  121. public static ResponseEntity<JSONObject> putNative(String url, JSONObject variables, JSONObject params) {
  122. return request(url, HttpMethod.PUT, variables, params);
  123. }
  124. /**
  125. * 发送 delete 请求
  126. */
  127. public static JSONObject delete(String url) {
  128. return deleteNative(url, null, null).getBody();
  129. }
  130. /**
  131. * 发送 delete 请求
  132. */
  133. public static JSONObject delete(String url, JSONObject variables, JSONObject params) {
  134. return deleteNative(url, variables, params).getBody();
  135. }
  136. /**
  137. * 发送 delete 请求,返回原生 ResponseEntity 对象
  138. */
  139. public static ResponseEntity<JSONObject> deleteNative(String url, JSONObject variables, JSONObject params) {
  140. return request(url, HttpMethod.DELETE, null, variables, params, JSONObject.class);
  141. }
  142. /**
  143. * 发送请求
  144. */
  145. public static ResponseEntity<JSONObject> request(String url, HttpMethod method, JSONObject variables, JSONObject params) {
  146. return request(url, method, getHeaderApplicationJson(), variables, params, JSONObject.class);
  147. }
  148. /**
  149. * 发送请求
  150. */
  151. public static ResponseEntity<JSONObject> request(String url, HttpMethod method, JSONObject variables, JSONArray params) {
  152. return request(url, method, getHeaderApplicationJson(variables), variables, params, JSONObject.class);
  153. }
  154. /**
  155. * 发送请求
  156. *
  157. * @param url 请求地址
  158. * @param method 请求方式
  159. * @param headers 请求头 可空
  160. * @param variables 请求url参数 可空
  161. * @param params 请求body参数 可空
  162. * @param responseType 返回类型
  163. * @return ResponseEntity<responseType>
  164. */
  165. public static <T> ResponseEntity<T> request(String url, HttpMethod method, HttpHeaders headers, JSONObject variables, Object params, Class<T> responseType) {
  166. log.info(" RestUtil --- request --- url = " + url);
  167. if (StringUtils.isEmpty(url)) {
  168. throw new RuntimeException("url 不能为空");
  169. }
  170. if (method == null) {
  171. throw new RuntimeException("method 不能为空");
  172. }
  173. if (headers == null) {
  174. headers = new HttpHeaders();
  175. }
  176. // 请求体
  177. String body = "";
  178. if (params != null) {
  179. if (params instanceof JSONObject) {
  180. body = ((JSONObject) params).toJSONString();
  181. } else if (params instanceof JSONArray) {
  182. body = ((JSONArray) params).toJSONString();
  183. } else {
  184. body = params.toString();
  185. }
  186. }
  187. // 拼接 url 参数
  188. if (variables != null) {
  189. url += ("?" + asUrlVariables(variables));
  190. }
  191. // 发送请求
  192. HttpEntity<String> request = new HttpEntity<>(body, headers);
  193. return RT.exchange(url, method, request, responseType);
  194. }
  195. /**
  196. * 获取JSON请求头
  197. */
  198. public static HttpHeaders getHeaderApplicationJson() {
  199. return getHeader(MediaType.APPLICATION_JSON_UTF8_VALUE);
  200. }
  201. /**
  202. * 获取JSON请求头
  203. */
  204. public static HttpHeaders getHeaderApplicationJson(JSONObject jsonObject) {
  205. HttpHeaders headers = getHeader(MediaType.APPLICATION_JSON_UTF8_VALUE);
  206. if (null != jsonObject && null != jsonObject.getString(HttpHeaders.COOKIE)) {
  207. headers.add(HttpHeaders.COOKIE, jsonObject.getString(HttpHeaders.COOKIE));
  208. }
  209. return headers;
  210. }
  211. /**
  212. * 获取请求头
  213. */
  214. public static HttpHeaders getHeader(String mediaType) {
  215. HttpHeaders headers = new HttpHeaders();
  216. headers.setContentType(MediaType.parseMediaType(mediaType));
  217. headers.add("Accept", mediaType);
  218. return headers;
  219. }
  220. /**
  221. * 将 JSONObject 转为 a=1&b=2&c=3...&n=n 的形式
  222. */
  223. public static String asUrlVariables(JSONObject variables) {
  224. Map<String, Object> source = variables.getInnerMap();
  225. Iterator<String> it = source.keySet().iterator();
  226. StringBuilder urlVariables = new StringBuilder();
  227. while (it.hasNext()) {
  228. String key = it.next();
  229. String value = "";
  230. Object object = source.get(key);
  231. if (object != null) {
  232. if (!StringUtils.isEmpty(object.toString())) {
  233. value = object.toString();
  234. }
  235. }
  236. urlVariables.append("&").append(key).append("=").append(value);
  237. }
  238. // 去掉第一个&
  239. return urlVariables.substring(1);
  240. }
  241. }