fix(API数据源): 请求 form-data 失败

This commit is contained in:
taojinlong 2022-09-27 11:11:57 +08:00
parent 2532483a85
commit 5f09346929
3 changed files with 28 additions and 27 deletions

View File

@ -1,16 +1,15 @@
package io.dataease.controller.request.datasource; package io.dataease.controller.request.datasource;
import com.google.gson.JsonObject; import com.alibaba.fastjson.JSONObject;
import lombok.Data; import lombok.Data;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class ApiDefinitionRequest { public class ApiDefinitionRequest {
private List<Map<String, String>> headers = new ArrayList<>(); private List<Map<String, String>> headers = new ArrayList<>();
private Map<String, Object> body = new HashMap<>(); private JSONObject body = new JSONObject();
private AuthManager authManager = new AuthManager(); private AuthManager authManager = new AuthManager();

View File

@ -5,8 +5,9 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.*; import com.google.common.reflect.TypeToken;
import com.google.gson.reflect.TypeToken; import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.dataease.controller.sys.response.BasicInfo; import io.dataease.controller.sys.response.BasicInfo;
import io.dataease.dto.dataset.DatasetTableFieldDTO; import io.dataease.dto.dataset.DatasetTableFieldDTO;
import io.dataease.plugins.common.dto.datasource.TableDesc; import io.dataease.plugins.common.dto.datasource.TableDesc;
@ -161,11 +162,12 @@ public class ApiProvider extends Provider {
if (StringUtils.equalsAny(type, "Form_Data", "WWW_FORM")) { if (StringUtils.equalsAny(type, "Form_Data", "WWW_FORM")) {
if (apiDefinitionRequest.getBody().get("kvs") != null) { if (apiDefinitionRequest.getBody().get("kvs") != null) {
Map<String, String> body = new HashMap<>(); Map<String, String> body = new HashMap<>();
JsonArray kvsArr = JsonParser.parseString(apiDefinitionRequest.getBody().get("kvs").toString()).getAsJsonArray(); JSONObject bodyObj = JSONObject.parseObject(apiDefinitionRequest.getBody().toString());
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
for (int i = 0; i < kvsArr.size(); i++) { for (int i = 0; i < kvsArr.size(); i++) {
JsonObject kv = kvsArr.get(i).getAsJsonObject(); JSONObject kv = kvsArr.getJSONObject(i);
if (kv.get("name") != null) { if (kv.containsKey("name")) {
body.put(kv.get("name").getAsString(), kv.get("value").getAsString()); body.put(kv.getString("name"), kv.getString("value"));
} }
} }
response = HttpClientUtil.post(apiDefinition.getUrl(), body, httpClientConfig); response = HttpClientUtil.post(apiDefinition.getUrl(), body, httpClientConfig);
@ -216,14 +218,14 @@ public class ApiProvider extends Provider {
try { try {
JSONArray jsonArray = jsonObject.getJSONArray(s); JSONArray jsonArray = jsonObject.getJSONArray(s);
List<JSONObject> childrenField = new ArrayList<>(); List<JSONObject> childrenField = new ArrayList<>();
for (Object object: jsonArray) { for (Object object : jsonArray) {
JSONObject.parseObject(object.toString()); JSONObject.parseObject(object.toString());
handleStr(apiDefinition, JSON.toJSONString(object, SerializerFeature.WriteMapNullValue), childrenField, rootPath + "." + s + "[*]"); handleStr(apiDefinition, JSON.toJSONString(object, SerializerFeature.WriteMapNullValue), childrenField, rootPath + "." + s + "[*]");
} }
o.put("children", childrenField); o.put("children", childrenField);
o.put("childrenDataType", "LIST"); o.put("childrenDataType", "LIST");
}catch (Exception e){ } catch (Exception e) {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
array.add(StringUtils.isNotEmpty(jsonObject.getString(s)) ? jsonObject.getString(s) : ""); array.add(StringUtils.isNotEmpty(jsonObject.getString(s)) ? jsonObject.getString(s) : "");
o.put("value", array); o.put("value", array);
@ -295,11 +297,11 @@ public class ApiProvider extends Provider {
} }
static void mergeField(JSONObject field, JSONObject item){ static void mergeField(JSONObject field, JSONObject item) {
if(item.getJSONArray("children") != null ){ if (item.getJSONArray("children") != null) {
JSONArray itemChildren = item.getJSONArray("children"); JSONArray itemChildren = item.getJSONArray("children");
JSONArray fieldChildren = field.getJSONArray("children"); JSONArray fieldChildren = field.getJSONArray("children");
if(fieldChildren == null){ if (fieldChildren == null) {
fieldChildren = new JSONArray(); fieldChildren = new JSONArray();
} }
for (Object itemChild : itemChildren) { for (Object itemChild : itemChildren) {
@ -307,12 +309,12 @@ public class ApiProvider extends Provider {
JSONObject itemChildObject = JSONObject.parseObject(itemChild.toString()); JSONObject itemChildObject = JSONObject.parseObject(itemChild.toString());
for (Object fieldChild : fieldChildren) { for (Object fieldChild : fieldChildren) {
JSONObject fieldChildObject = JSONObject.parseObject(fieldChild.toString()); JSONObject fieldChildObject = JSONObject.parseObject(fieldChild.toString());
if(itemChildObject.getString("jsonPath").equals(fieldChildObject.getString("jsonPath"))){ if (itemChildObject.getString("jsonPath").equals(fieldChildObject.getString("jsonPath"))) {
mergeField(fieldChildObject, itemChildObject); mergeField(fieldChildObject, itemChildObject);
hasKey = true; hasKey = true;
} }
} }
if(!hasKey){ if (!hasKey) {
fieldChildren.add(itemChild); fieldChildren.add(itemChild);
} }
} }
@ -325,7 +327,7 @@ public class ApiProvider extends Provider {
array.add(item.getJSONArray("value").get(0).toString()); array.add(item.getJSONArray("value").get(0).toString());
field.put("value", array); field.put("value", array);
} }
if(CollectionUtils.isNotEmpty(field.getJSONArray("children"))&& CollectionUtils.isNotEmpty(item.getJSONArray("children"))){ if (CollectionUtils.isNotEmpty(field.getJSONArray("children")) && CollectionUtils.isNotEmpty(item.getJSONArray("children"))) {
JSONArray fieldChildren = field.getJSONArray("children"); JSONArray fieldChildren = field.getJSONArray("children");
JSONArray itemChildren = item.getJSONArray("children"); JSONArray itemChildren = item.getJSONArray("children");
@ -335,11 +337,11 @@ public class ApiProvider extends Provider {
JSONObject find = null; JSONObject find = null;
for (Object itemChild : itemChildren) { for (Object itemChild : itemChildren) {
JSONObject itemObject = JSONObject.parseObject(itemChild.toString()); JSONObject itemObject = JSONObject.parseObject(itemChild.toString());
if(jsonObject.getString("jsonPath").equals(itemObject.getString("jsonPath"))){ if (jsonObject.getString("jsonPath").equals(itemObject.getString("jsonPath"))) {
find = itemObject; find = itemObject;
} }
} }
if(find != null){ if (find != null) {
mergeValue(jsonObject, apiDefinition, find); mergeValue(jsonObject, apiDefinition, find);
} }
fieldArrayChildren.add(jsonObject); fieldArrayChildren.add(jsonObject);

View File

@ -184,7 +184,7 @@ public class DatasourceService {
for (int i = 0; i < apiDefinitionList.size(); i++) { for (int i = 0; i < apiDefinitionList.size(); i++) {
String status = null; String status = null;
if(apiItemStatuses.get(apiDefinitionList.get(i).getName()) != null){ if (apiItemStatuses.get(apiDefinitionList.get(i).getName()) != null) {
status = apiItemStatuses.get(apiDefinitionList.get(i).getName()).getAsString(); status = apiItemStatuses.get(apiDefinitionList.get(i).getName()).getAsString();
} }
apiDefinitionList.get(i).setStatus(status); apiDefinitionList.get(i).setStatus(status);
@ -304,9 +304,9 @@ public class DatasourceService {
return ResultHolder.success(datasourceDTO); return ResultHolder.success(datasourceDTO);
} }
if (success > 0 && success < apiDefinitionList.size()) { if (success > 0 && success < apiDefinitionList.size()) {
return ResultHolder.error(Translator.get("I18N_DS_INVALID_TABLE") , datasourceDTO); return ResultHolder.error(Translator.get("I18N_DS_INVALID_TABLE"), datasourceDTO);
} }
return ResultHolder.error(Translator.get("I18N_DS_INVALID") , datasourceDTO); return ResultHolder.error(Translator.get("I18N_DS_INVALID"), datasourceDTO);
} }
return ResultHolder.success(datasourceDTO); return ResultHolder.success(datasourceDTO);
} catch (Exception e) { } catch (Exception e) {
@ -547,7 +547,7 @@ public class DatasourceService {
type = parameter.getParamValue(); type = parameter.getParamValue();
} }
} }
if(!changeDsCheckTime){ if (!changeDsCheckTime) {
return; return;
} }
addJob(type, interval); addJob(type, interval);
@ -555,7 +555,7 @@ public class DatasourceService {
private void addJob(String type, Integer interval) { private void addJob(String type, Integer interval) {
String cron = ""; String cron = "";
switch (type){ switch (type) {
case "hour": case "hour":
cron = "0 0 0/hour * * ? *".replace("hour", interval.toString()); cron = "0 0 0/hour * * ? *".replace("hour", interval.toString());
break; break;
@ -572,12 +572,12 @@ public class DatasourceService {
globalTask.setStartTime(System.currentTimeMillis()); globalTask.setStartTime(System.currentTimeMillis());
try { try {
scheduleService.addSchedule(globalTask); scheduleService.addSchedule(globalTask);
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void initDsCheckJob(){ public void initDsCheckJob() {
BasicInfo basicInfo = systemParameterService.basicInfo(); BasicInfo basicInfo = systemParameterService.basicInfo();
addJob(basicInfo.getDsCheckIntervalType(), Integer.valueOf(basicInfo.getDsCheckInterval())); addJob(basicInfo.getDsCheckIntervalType(), Integer.valueOf(basicInfo.getDsCheckInterval()));
} }