Browse Source

Merge branch 'data-bridge' of wangfan/adweb3-server into master

wangfan 6 months ago
parent
commit
13a822f398

+ 25 - 3
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/util/RestTemplateUtil.java

@@ -3,8 +3,10 @@ package org.jeecg.modules.adweb.common.util;
 import lombok.extern.slf4j.Slf4j;
 
 import org.apache.commons.codec.binary.Base64;
-import org.springframework.http.HttpHeaders;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.http.*;
 import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 
 import java.nio.charset.StandardCharsets;
@@ -45,7 +47,26 @@ public class RestTemplateUtil {
                 Map.of(HttpHeaders.AUTHORIZATION, "Basic " + new String(encodedAuth)));
     }
 
-    public static RestTemplate getRestTemplate(
+    /** HTTP POST请求读取对象 */
+    public static <T> T postForObject(
+            RestTemplate restTemplate,
+            String url,
+            Object request,
+            ParameterizedTypeReference<T> responseType)
+            throws RestClientException {
+        RequestEntity<?> requestEntity = RequestEntity.post(url).body(request);
+        return restTemplate.exchange(requestEntity, responseType).getBody();
+    }
+
+    /** HTTP GET请求读取对象 */
+    public static <T> T getForObject(
+            RestTemplate restTemplate, String url, ParameterizedTypeReference<T> responseType)
+            throws RestClientException {
+        RequestEntity<?> requestEntity = RequestEntity.get(url).build();
+        return restTemplate.exchange(requestEntity, responseType).getBody();
+    }
+
+    private static RestTemplate getRestTemplate(
             int connectTimeoutSeconds, int readTimeoutSeconds, Map<String, String> httpHeaders) {
         // 1. 请求超时
         SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
@@ -59,7 +80,8 @@ public class RestTemplateUtil {
                 .getInterceptors()
                 .add(
                         ((request, body, execution) -> {
-                            httpHeaders.entrySet().stream()
+                            httpHeaders
+                                    .entrySet()
                                     .forEach(
                                             entry ->
                                                     request.getHeaders()