|
@@ -1,41 +1,42 @@
|
|
|
package com.scbfkj.uni.process;
|
|
|
|
|
|
-import com.scbfkj.uni.exceptions.ConnectionNotFoundException;
|
|
|
-import com.scbfkj.uni.library.DataFormatUtil;
|
|
|
-import com.scbfkj.uni.library.UniReturnUtil;
|
|
|
-import com.scbfkj.uni.system.Config;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.http.HttpEntity;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.*;
|
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.scbfkj.uni.exceptions.ConnectionNotFoundException;
|
|
|
+import com.scbfkj.uni.library.DataFormatUtil;
|
|
|
+import com.scbfkj.uni.library.UniReturnUtil;
|
|
|
+import com.scbfkj.uni.system.Config;
|
|
|
+
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+
|
|
|
/**
|
|
|
* Web API调用类
|
|
|
*/
|
|
|
public class Web {
|
|
|
-
|
|
|
private static final DataBase DATA_BASE = new DataBase();
|
|
|
-
|
|
|
+
|
|
|
private RestTemplate restTemplate; // Web API请求对象
|
|
|
-
|
|
|
+
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Web.class);
|
|
|
-
|
|
|
+
|
|
|
private String webapiURL;
|
|
|
-
|
|
|
+
|
|
|
private HttpHeaders webApiHeader;
|
|
|
+
|
|
|
private String webapiMethod;
|
|
|
-
|
|
|
+
|
|
|
private Map<String, Object> baseHeader = new HashMap<>();
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 根据数据源ID执行Web API
|
|
|
*
|
|
@@ -43,12 +44,14 @@ public class Web {
|
|
|
* @param headers 请求头信息,可选
|
|
|
* @param method HTTP请求方法,默认为"post"
|
|
|
* @param defaultBody 请求体内容,默认为空
|
|
|
+ *
|
|
|
* @return Web API执行结果
|
|
|
+ *
|
|
|
* @throws Exception 抛出异常的情况
|
|
|
*/
|
|
|
- public Map<String, Object> execWebApiByDataSourceId(String dataSourceId, Object headers, String method, Object defaultBody) throws Exception {
|
|
|
+ public Map<String, Object> execWebApiByDataSourceId(
|
|
|
+ String dataSourceId, Object headers, String method, Object defaultBody) throws Exception {
|
|
|
Map<String, Object> config = queryConnectionStr(dataSourceId); // 查询数据源配置
|
|
|
-
|
|
|
String host;
|
|
|
if (Objects.isNull(headers)) {
|
|
|
headers = config.get("headers");
|
|
@@ -62,7 +65,7 @@ public class Web {
|
|
|
host = config.get("host").toString();
|
|
|
return execWebApi(headers, method, defaultBody, host);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 执行Web API
|
|
|
*
|
|
@@ -70,19 +73,16 @@ public class Web {
|
|
|
* @param method HTTP请求方法
|
|
|
* @param defaultBody 请求体内容
|
|
|
* @param url 请求的URL地址
|
|
|
+ *
|
|
|
* @return 执行结果
|
|
|
*/
|
|
|
public Map<String, Object> execWebApi(Object headers, String method, Object defaultBody, String url) {
|
|
|
-
|
|
|
- if (Config.isDebug()) {
|
|
|
- logger.info("headers: {}", headers);
|
|
|
- logger.info("method: {}", method);
|
|
|
- logger.info("body: {}", defaultBody);
|
|
|
- }
|
|
|
-
|
|
|
+ logger.info("headers: {}", headers);
|
|
|
+ logger.info("method: {}", method);
|
|
|
+ logger.info("body: {}", defaultBody);
|
|
|
+ logger.info("url: {}", url);
|
|
|
Map<String, Object> restTemplateResult = initWebApiParams(headers, method, url); // 初始化Web API参数
|
|
|
-
|
|
|
- if (!"0".equals(restTemplateResult.get("code"))) {
|
|
|
+ if (! "0".equals(restTemplateResult.get("code"))) {
|
|
|
return UniReturnUtil.fail(restTemplateResult.get("message").toString());
|
|
|
}
|
|
|
if (headers instanceof Map<?, ?> temeMap) {
|
|
@@ -92,21 +92,19 @@ public class Web {
|
|
|
webApiHeader.add((String) entry.getKey(), DataFormatUtil.toString(entry.getValue()));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
try {
|
|
|
-
|
|
|
Map<String, Object> result = sendWebApiRequest(defaultBody);
|
|
|
-
|
|
|
return UniReturnUtil.success(result);
|
|
|
} catch (Exception e) {
|
|
|
return UniReturnUtil.fail("restTemplate执行异常" + UniReturnUtil.getMessage(e));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 发送Web API请求
|
|
|
*
|
|
|
* @param defaultBody 请求体内容
|
|
|
+ *
|
|
|
* @return 请求结果
|
|
|
*/
|
|
|
private Map<String, Object> sendWebApiRequest(Object defaultBody) {
|
|
@@ -114,7 +112,14 @@ public class Web {
|
|
|
HttpEntity<Object> request = new HttpEntity<>(defaultBody, webApiHeader);
|
|
|
Object responseEntity = null;
|
|
|
switch (webapiMethod) {
|
|
|
- case "get" -> responseEntity = restTemplate.getForObject(webapiURL, Object.class, request);
|
|
|
+ case "get" -> {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ webApiHeader.forEach((key, value) -> headers.set(key, value.toString()));
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(headers);
|
|
|
+ ResponseEntity<JsonNode> response =
|
|
|
+ restTemplate.exchange(webapiURL, HttpMethod.GET, entity, JsonNode.class);
|
|
|
+ responseEntity = response.getBody();
|
|
|
+ }
|
|
|
case "put" -> restTemplate.put(webapiURL, request);
|
|
|
case "delete" -> restTemplate.delete(webapiURL, request);
|
|
|
default -> responseEntity = restTemplate.postForObject(webapiURL, request, Object.class);
|
|
@@ -134,18 +139,18 @@ public class Web {
|
|
|
return UniReturnUtil.fail("发送请求失败 : ".concat(UniReturnUtil.getMessage(e)));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 初始化Web API参数
|
|
|
*
|
|
|
* @param headers 请求头信息
|
|
|
* @param method HTTP请求方法
|
|
|
* @param url 请求的URL地址
|
|
|
+ *
|
|
|
* @return 初始化结果
|
|
|
*/
|
|
|
private Map<String, Object> initWebApiParams(Object headers, String method, String url) {
|
|
|
try {
|
|
|
-
|
|
|
if (restTemplate == null) {
|
|
|
webapiURL = url;
|
|
|
if (Objects.isNull(webapiURL)) {
|
|
@@ -158,7 +163,7 @@ public class Web {
|
|
|
}
|
|
|
if (tempHeaders instanceof Map<?, ?> temp) {
|
|
|
baseHeader = (Map<String, Object>) temp;
|
|
|
- if (!baseHeader.isEmpty()) {
|
|
|
+ if (! baseHeader.isEmpty()) {
|
|
|
for (Map.Entry<String, Object> entry : baseHeader.entrySet()) {
|
|
|
webApiHeader.add(entry.getKey(), DataFormatUtil.toString(entry.getValue()));
|
|
|
}
|
|
@@ -177,13 +182,14 @@ public class Web {
|
|
|
}
|
|
|
return UniReturnUtil.success(null);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 查询数据源连接字符串
|
|
|
*
|
|
|
* @param datasourceId 数据源ID
|
|
|
+ *
|
|
|
* @return 数据源配置信息
|
|
|
+ *
|
|
|
* @throws Exception 抛出异常的情况
|
|
|
*/
|
|
|
private static Map<String, Object> queryConnectionStr(String datasourceId) throws Exception {
|