扩展存放业务参数

develop
tanghc 3 years ago
parent efe46ff3e1
commit f0c01dfd16
  1. 8
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ApiConfig.java
  2. 25
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/BizContentHandler.java
  3. 9
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/result/GatewayResultExecutor.java
  4. 4
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.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 ---------
/**

@ -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<String, Object> serviceData, JSONObject serviceObj, ApiParam apiParam, ServerWebExchange request);
}

@ -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<ServerWebExchange
return this.merge(exchange, jsonObject);
}
@Override
protected void handleBizContent(Map<String, Object> serviceData, JSONObject serviceObj, ApiParam apiParam, ServerWebExchange request) {
ApiConfig.getInstance().getBizContentHandler().handle(serviceData, serviceObj, apiParam, request);
}
}

@ -179,10 +179,12 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
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<String, Object> serviceData, JSONObject serviceObj, ApiParam apiParam, T request);
/**
* 该路由是否合并结果

Loading…
Cancel
Save