|
@@ -31,7 +31,7 @@ public class Web {
|
|
|
return new DefaultPooledObject<>(web);
|
|
|
}
|
|
|
});
|
|
|
- private final ObjectPool<WebClient> clientPool = new GenericObjectPool<>(new BasePooledObjectFactory<>() {
|
|
|
+ private final static ObjectPool<WebClient> clientPool = new GenericObjectPool<>(new BasePooledObjectFactory<>() {
|
|
|
|
|
|
@Override
|
|
|
public WebClient create() throws Exception {
|
|
@@ -55,29 +55,15 @@ public class Web {
|
|
|
|
|
|
private Map<String, Object> baseHeader = new HashMap<>();
|
|
|
|
|
|
- /**
|
|
|
- * webapi 执行入口函数
|
|
|
- */
|
|
|
- public Map<String, Object> exec(Object header, String method, Object defaultBody, String connectConfigStr) throws Exception {
|
|
|
- WebClient webClient = clientPool.borrowObject();
|
|
|
- HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
|
|
|
- Map<?, ?> connectConfig = DataFormatUtil.toMap(connectConfigStr);
|
|
|
- String url = Objects.nonNull(connectConfig.get("url")) ? connectConfig.get("url").toString() : null;
|
|
|
- if (Objects.isNull(url)) {
|
|
|
- return UniReturnUtil.fail("webapi请求地址为空");
|
|
|
- }
|
|
|
- return UniReturnUtil.fail("");
|
|
|
- }
|
|
|
|
|
|
- public Map<String, Object> execWebApi(Object header, String method, Object defaultBody, String connectConfig) {
|
|
|
- Map<String, Object> connectConfigMaps = (Map<String, Object>) DataFormatUtil.toMap(connectConfig);
|
|
|
- Map<String, Object> restTemplateResult = initWebApiParams(connectConfigMaps); // 通过连接配置 获取restTemplate
|
|
|
+ public Map<String, Object> execWebApi(Object headers, String method, Object defaultBody, String url) {
|
|
|
+ Map<String, Object> restTemplateResult = initWebApiParams(headers,method,url); // 通过连接配置 获取restTemplate
|
|
|
|
|
|
if (!"0".equals(restTemplateResult.get("code"))) {
|
|
|
return UniReturnUtil.fail(restTemplateResult.get("message").toString());
|
|
|
}
|
|
|
- if (header instanceof Map) {
|
|
|
- Map<String, Object> temeMap = (Map) header;
|
|
|
+ if (headers instanceof Map) {
|
|
|
+ Map<String, Object> temeMap = (Map) headers;
|
|
|
if (!temeMap.isEmpty()) {
|
|
|
temeMap.putAll(baseHeader);
|
|
|
webApiHeader.clear();
|
|
@@ -86,17 +72,11 @@ public class Web {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if (StringUtils.hasLength(method)) webapiMethod = method.toLowerCase();
|
|
|
try {
|
|
|
- List<Map<String, Object>> returnList = new ArrayList<>();
|
|
|
- if (defaultBody instanceof List<?> tempList) {
|
|
|
- for (Object o : tempList) {
|
|
|
- returnList.add(sendWebApiRequest(o));
|
|
|
- }
|
|
|
- } else {
|
|
|
- returnList.add(sendWebApiRequest(defaultBody));
|
|
|
- }
|
|
|
- return (defaultBody instanceof List<?>) ? UniReturnUtil.success(returnList) : returnList.get(0);
|
|
|
+
|
|
|
+ Map<String, Object> result = sendWebApiRequest(defaultBody);
|
|
|
+
|
|
|
+ return UniReturnUtil.success(result) ;
|
|
|
} catch (Exception e) {
|
|
|
return UniReturnUtil.fail("restTemplate执行异常" + UniReturnUtil.getMessage(e));
|
|
|
}
|
|
@@ -125,19 +105,15 @@ public class Web {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private Map<String, Object> initWebApiParams(Map<String, Object> connectConfig) {
|
|
|
+ private Map<String, Object> initWebApiParams(Object headers,String method,String url) {
|
|
|
try {
|
|
|
- String configStr = DataFormatUtil.toString(connectConfig);
|
|
|
|
|
|
if (restTemplate == null) {
|
|
|
- if (Objects.isNull(connectConfig)) {
|
|
|
- return UniReturnUtil.fail("webapi请求配置参数为空");
|
|
|
- }
|
|
|
- webapiURL = Objects.nonNull(connectConfig.get("url")) ? connectConfig.get("url").toString() : null;
|
|
|
+ webapiURL = url;
|
|
|
if (Objects.isNull(webapiURL)) {
|
|
|
return UniReturnUtil.fail("webapi请求地址为空");
|
|
|
}
|
|
|
- Object tempHeaders = connectConfig.get("headers"); //需确定返回的是一个map对象还是字符串 headers: {ContentType:"JSON"}
|
|
|
+ Object tempHeaders = headers; //需确定返回的是一个map对象还是字符串 headers: {ContentType:"JSON"}
|
|
|
webApiHeader = new HttpHeaders();
|
|
|
if (Objects.isNull(tempHeaders)) {
|
|
|
webApiHeader.setContentType(MediaType.APPLICATION_JSON);
|
|
@@ -150,7 +126,7 @@ public class Web {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- Object webMethod = connectConfig.get("method"); //需确定返回的是一个map对象还是字符串 headers: {ContentType:"JSON"}
|
|
|
+ Object webMethod = method; //需确定返回的是一个map对象还是字符串 headers: {ContentType:"JSON"}
|
|
|
if (Objects.nonNull(webMethod)) {
|
|
|
webapiMethod = webMethod.toString().toLowerCase();
|
|
|
} else {
|