|
@@ -3,6 +3,7 @@ package com.hrsk.cloud.eg.domain.api.resolver.payload;
|
|
|
import com.alibaba.cola.exception.BizException;
|
|
|
import com.google.gson.*;
|
|
|
import com.hrsk.cloud.eg.domain.api.ApiContext;
|
|
|
+import com.hrsk.cloud.eg.domain.api.ApiResult;
|
|
|
import com.hrsk.cloud.eg.domain.api.ValueObject;
|
|
|
import com.hrsk.cloud.eg.domain.common.utils.GsonUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -34,6 +35,49 @@ public class ApiJsonPayloadResolver implements ApiPayloadResolver {
|
|
|
return jsonElement.toString();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object resolve(String jsonTemplate, ApiResult context) {
|
|
|
+ JsonElement jsonElement = GsonUtils.fromJson(jsonTemplate, JsonElement.class);
|
|
|
+ traverseJson(jsonElement,jsonElement,null,context,0);
|
|
|
+ return jsonElement.toString();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 遍历json
|
|
|
+ * @param jsonElement json元素
|
|
|
+ * @param context 上下文
|
|
|
+ * @param deep 深度
|
|
|
+ */
|
|
|
+ public void traverseJson(JsonElement jsonElement, JsonElement currentElement,String currentKey,ApiResult context,Integer deep) {
|
|
|
+ deep++;
|
|
|
+ if(deep >= MAX_DEEP){
|
|
|
+ throw new BizException(String.format("请求payload的json配置深度达到允许的最大深度[%s]!",MAX_DEEP));
|
|
|
+ }
|
|
|
+ if (jsonElement.isJsonObject()) {
|
|
|
+ JsonObject jsonObject = jsonElement.getAsJsonObject();
|
|
|
+ for (String key : jsonObject.keySet()) {
|
|
|
+ traverseJson(jsonObject.get(key),jsonObject,key,context,deep);
|
|
|
+ }
|
|
|
+ } else if (jsonElement.isJsonArray()) {
|
|
|
+ JsonArray jsonArray = jsonElement.getAsJsonArray();
|
|
|
+ for (JsonElement elem : jsonArray) {
|
|
|
+ traverseJson(elem,currentElement,null,context,deep);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if(jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString()){
|
|
|
+ String value = jsonElement.getAsString();
|
|
|
+ if(value.startsWith(CONFIG_PREFIX_SIGN)&&value.endsWith(CONFIG_END_SIGN)){
|
|
|
+ JsonPrimitive newElement = parseSpelConfigToRealValue(jsonElement,context);
|
|
|
+ if(StringUtils.isNotBlank(currentKey)){
|
|
|
+ currentElement.getAsJsonObject().add(currentKey,newElement);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 遍历json
|
|
|
* @param jsonElement json元素
|
|
@@ -68,6 +112,27 @@ public class ApiJsonPayloadResolver implements ApiPayloadResolver {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 将spel表达式转换成真实值
|
|
|
+ * @param jsonElement json元素
|
|
|
+ */
|
|
|
+ private JsonPrimitive parseSpelConfigToRealValue(JsonElement jsonElement, ApiResult context){
|
|
|
+ String value = jsonElement.getAsString();
|
|
|
+ value = StringUtils.replace(value,CONFIG_PREFIX_SIGN,"{",3);
|
|
|
+ ValueObject valueObject = new Gson().fromJson(value,ValueObject.class);
|
|
|
+ Object realValue = valueObject.compile(context).getRealValue();
|
|
|
+ if(realValue instanceof Number){
|
|
|
+ return new JsonPrimitive((Number)realValue);
|
|
|
+ }else if(realValue instanceof String){
|
|
|
+ return new JsonPrimitive((String)realValue);
|
|
|
+ }else if(realValue instanceof Boolean){
|
|
|
+ return new JsonPrimitive((Boolean)realValue);
|
|
|
+ }else if(realValue instanceof Character){
|
|
|
+ return new JsonPrimitive((Character)realValue);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 将spel表达式转换成真实值
|
|
|
* @param jsonElement json元素
|