From f0c01dfd16de8d62debd58e4089f208502e2fbc1 Mon Sep 17 00:00:00 2001 From: tanghc Date: Wed, 7 Jul 2021 10:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=AD=98=E6=94=BE=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sop/gatewaycommon/bean/ApiConfig.java | 8 ++++++ .../gateway/result/BizContentHandler.java | 25 +++++++++++++++++++ .../gateway/result/GatewayResultExecutor.java | 9 ++++--- .../result/BaseExecutorAdapter.java | 4 ++- 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/BizContentHandler.java 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); + /** * 该路由是否合并结果