http 接口 请求 优化 ,post application/x-www-form-urlencoded 问题修复

This commit is contained in:
2025-02-13 22:47:20 +08:00
parent 2480af577b
commit 2d4ddc2857
3 changed files with 50 additions and 12 deletions

View File

@@ -32,6 +32,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.client.AsyncRestTemplate;
@@ -244,20 +246,49 @@ public class InterfaceServiceImpl extends ServiceImpl<InterfaceMapper, Interface
// 请求参数中的变量赋值
String requestBody = setRequestBodyParams(interfaceInfo.getRequestBody(), inputParam);
if(HttpMethod.POST.name().equals(interfaceInfo.getMethod())){
HttpHeaders httpHeaders = new HttpHeaders();
// 设置请求头
httpHeaders.setAll(JSONObject.parseObject(interfaceInfo.getRequestHeaders(), Map.class));
// 封装请求体
JSONObject body = JSONObject.parseObject(requestBody);
// 封装参数和头信息
HttpEntity<JSONObject> httpEntity = new HttpEntity(body, httpHeaders);
// 发送请求
if(callType == 2){
responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
} else {
listenableFuture = asyncRestTemplate.postForEntity(url, httpEntity, String.class);
final String requestHeaders = interfaceInfo.getRequestHeaders();
if(requestHeaders!=null && requestHeaders.contains(MediaType.APPLICATION_JSON_VALUE)) {
HttpHeaders httpHeaders = new HttpHeaders();
// 设置请求头
httpHeaders.setAll(JSONObject.parseObject(requestHeaders, Map.class));
// 封装请求体
JSONObject body = JSONObject.parseObject(requestBody);
// 封装参数和头信息
HttpEntity<JSONObject> httpEntity = new HttpEntity(body, httpHeaders);
// 发送请求
if (callType == 2) {
responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
} else {
listenableFuture = asyncRestTemplate.postForEntity(url, httpEntity, String.class);
}
}else{
HttpHeaders httpHeaders = new HttpHeaders();
// 设置请求头
httpHeaders.setAll(JSONObject.parseObject(interfaceInfo.getRequestHeaders(), Map.class));
// 封装请求体
JSONObject body = JSONObject.parseObject(requestBody);
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.setAll(body);
// 封装参数和头信息
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity(bodyMap, httpHeaders);
// 发送请求
if (callType == 2) {
// 这里调用还是报错 ,已修复
// post http://47.99.93.74:8090/mockapi/hello2?name={name} 会报错
// java.lang.IllegalArgumentException: Not enough variable values available to expand 'name'
responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
} else {
listenableFuture = asyncRestTemplate.postForEntity(url, httpEntity, String.class);
}
}
} else if(HttpMethod.GET.name().equals(interfaceInfo.getMethod())){
// 封装uri地址路径变量
Map<String, Object> uriVariables = new HashMap<>();

View File

@@ -182,6 +182,9 @@ public class CommonServiceImpl implements CommonService {
map.put(key,value);
}
// 决策选项 测试时 ,这个方法 没被调用
@Override
public Map<String, Object> getFields(List<Field> fields, Map<String, Object> inputParam) {
logger.info("start getEngineField, fields:{},inputParam:{}", JSONObject.toJSONString(fields), JSONObject.toJSONString(inputParam));

View File

@@ -162,6 +162,10 @@ public class JevalUtil {
Object v = variablesMap.get(key);
if(v!=null) {
String variableValue = CommonConst.SYMBOL_SINGLE_QUOTA+v.toString()+CommonConst.SYMBOL_SINGLE_QUOTA;
variablesMap.put(key, variableValue);
}else{
String variableValue = CommonConst.SYMBOL_SINGLE_QUOTA+CommonConst.SYMBOL_SINGLE_QUOTA;
variablesMap.put(key, variableValue);
}
}