Browse Source

Rest template utils

wfansh 6 months ago
parent
commit
7cd27398ee

+ 0 - 41
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/common/util/HttpClientUtils.java

@@ -1,41 +0,0 @@
-package org.jeecg.modules.adweb.common.util;
-
-import lombok.extern.slf4j.Slf4j;
-
-import okhttp3.*;
-
-import org.apache.commons.lang3.StringUtils;
-
-import java.io.IOException;
-import java.util.concurrent.TimeUnit;
-
-/**
- * 基于{@link OkHttpClient}的API访问工具
- *
- * @author wfansh
- */
-@Slf4j
-public class HttpClientUtils {
-
-    private static final MediaType MEDIA_TYPE_JSON =
-            MediaType.get("application/json; charset=utf-8");
-
-    private static OkHttpClient OK_HTTP_CLIENT =
-            new OkHttpClient.Builder()
-                    .connectTimeout(5, TimeUnit.MINUTES)
-                    .readTimeout(5, TimeUnit.MINUTES)
-                    .writeTimeout(5, TimeUnit.MINUTES)
-                    .build();
-
-    private static String postForObject(String url, String json) throws IOException {
-        RequestBody requestBody = RequestBody.create(json, MEDIA_TYPE_JSON);
-        Request request = new Request.Builder().url(url).post(requestBody).build();
-
-        try (Response response = OK_HTTP_CLIENT.newCall(request).execute()) {
-            return response.body().string();
-        } catch (IOException e) {
-            log.error(e.getMessage(), e);
-            return StringUtils.EMPTY;
-        }
-    }
-}

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

@@ -0,0 +1,73 @@
+package org.jeecg.modules.adweb.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+import okhttp3.*;
+
+import org.apache.commons.codec.binary.Base64;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.web.client.RestTemplate;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+
+/**
+ * {@link RestTemplate}初始化及工厂类
+ *
+ * @author wfansh
+ */
+@Slf4j
+public class RestTemplateUtil {
+
+    /**
+     * @return 通过Bearer token认证的 {@link RestTemplate}
+     */
+    public static RestTemplate getRestTemplate(
+            int connectTimeoutSeconds, int readTimeoutSeconds, String bearerToken) {
+        return getRestTemplate(
+                connectTimeoutSeconds,
+                readTimeoutSeconds,
+                Map.of(HttpHeaders.AUTHORIZATION, "Bearer " + bearerToken));
+    }
+
+    /**
+     * @return 通过userName + password认证的 {@link RestTemplate}
+     */
+    public static RestTemplate getRestTemplate(
+            int connectTimeoutSeconds, int readTimeoutSeconds, String userName, String password) {
+        String auth = userName + ":" + password;
+        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.US_ASCII));
+
+        return getRestTemplate(
+                connectTimeoutSeconds,
+                readTimeoutSeconds,
+                Map.of(HttpHeaders.AUTHORIZATION, "Basic " + new String(encodedAuth)));
+    }
+
+    public static RestTemplate getRestTemplate(
+            int connectTimeoutSeconds, int readTimeoutSeconds, Map<String, String> httpHeaders) {
+        // 1. 请求超时
+        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
+        requestFactory.setConnectTimeout(1000 * connectTimeoutSeconds);
+        requestFactory.setReadTimeout(1000 * readTimeoutSeconds);
+
+        RestTemplate restTemplate = new RestTemplate(requestFactory);
+
+        // 2. HTTP请求头
+        restTemplate
+                .getInterceptors()
+                .add(
+                        ((request, body, execution) -> {
+                            httpHeaders.entrySet().stream()
+                                    .forEach(
+                                            entry ->
+                                                    request.getHeaders()
+                                                            .add(entry.getKey(), entry.getValue()));
+
+                            return execution.execute(request, body);
+                        }));
+
+        return restTemplate;
+    }
+}