diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java index 93659911..bc5d21ef 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java @@ -1,5 +1,6 @@ package com.gitee.sop.gatewaycommon.bean; +import com.gitee.sop.gatewaycommon.gateway.result.BizContentHandler; import com.gitee.sop.gatewaycommon.gateway.result.GatewayResultExecutor; import com.gitee.sop.gatewaycommon.interceptor.RouteInterceptor; import com.gitee.sop.gatewaycommon.limit.DefaultLimitManager; @@ -143,6 +144,13 @@ public class ApiConfig { */ private MonitorManager monitorManager = new MonitorManager(); + /** + * 业务返回参数处理 + */ + private BizContentHandler bizContentHandler = (serviceData, serviceObj, apiParam, request) -> { + serviceData.putAll(serviceObj); + }; + // -------- fields --------- /** diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/BizContentHandler.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/BizContentHandler.java new file mode 100644 index 00000000..2ef05d80 --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/BizContentHandler.java @@ -0,0 +1,25 @@ +package com.gitee.sop.gatewaycommon.gateway.result; + +import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.gatewaycommon.param.ApiParam; +import org.springframework.web.server.ServerWebExchange; + +import java.util.Map; + +/** + * 处理业务数据 + * @author thc + */ +public interface BizContentHandler { + + /** + * 处理业务参数 + * @param serviceData 外层service数据 + * @param serviceObj 业务数据 + * @param apiParam apiParam + * @param request ServerWebExchange + * @author thc + * @date 2021/7/7 10:41 + */ + void handle(Map serviceData, JSONObject serviceObj, ApiParam apiParam, ServerWebExchange request); +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java index 08785bda..e45f2b75 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java @@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.gateway.result; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.DefaultRouteInterceptorContext; import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.exception.ApiException; @@ -16,14 +17,12 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.gateway.support.TimeoutException; import org.springframework.http.HttpStatus; -import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.UriUtils; import java.nio.charset.StandardCharsets; -import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Optional; @@ -96,4 +95,8 @@ public class GatewayResultExecutor extends BaseExecutorAdapter serviceData, JSONObject serviceObj, ApiParam apiParam, ServerWebExchange request) { + ApiConfig.getInstance().getBizContentHandler().handle(serviceData, serviceObj, apiParam, request); + } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java index fa2de3a7..0e48ce57 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java @@ -179,10 +179,12 @@ public abstract class BaseExecutorAdapter implements ResultExecutor serviceData.put(GATEWAY_CODE_NAME, errorMeta.getCode()); serviceData.put(GATEWAY_MSG_NAME, errorMeta.getError(getLocale(request)).getMsg()); JSONObject serviceObj = JSON.parseObject(serviceResult, Feature.OrderedField); - serviceData.putAll(serviceObj); + this.handleBizContent(serviceData, serviceObj, apiParam, request); return serviceData; } + protected abstract void handleBizContent(Map serviceData, JSONObject serviceObj, ApiParam apiParam, T request); + /** * 该路由是否合并结果