tanghc 4 years ago
parent c120788857
commit 57d0ccbede
  1. 20
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java

@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.gitee.sop.gatewaycommon.bean.ApiConfig;
import com.gitee.sop.gatewaycommon.bean.DefaultRouteInterceptorContext;
import com.gitee.sop.gatewaycommon.bean.Isv;
@ -20,6 +21,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
@ -94,7 +96,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
this.doAfterRoute(serviceResult, responseStatus, request);
String finalResult;
if (isMergeResult) {
JSONObject responseData = this.parseServiceResult(serviceResult, responseStatus, request);
Map<String, Object> responseData = this.parseServiceResult(serviceResult, responseStatus, request);
finalResult = this.merge(request, responseData);
} else {
finalResult = serviceResult;
@ -141,7 +143,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
* @param request 请求
* @return 返回JSONObject
*/
protected JSONObject parseServiceResult(String serviceResult, int responseStatus, T request) {
protected Map<String, Object> parseServiceResult(String serviceResult, int responseStatus, T request) {
ErrorEnum errorEnum = HTTP_STATUS_ERROR_ENUM_MAP.get(responseStatus);
if (errorEnum == null) {
// 其它异常不应该把异常信息告诉给客户端,将微服务内容设置成空的json
@ -149,15 +151,17 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
errorEnum = ErrorEnum.ISP_UNKNOWN_ERROR;
}
ErrorMeta errorMeta = errorEnum.getErrorMeta();
JSONObject responseData = JSON.parseObject(serviceResult);
Map<String, Object> serviceData = new LinkedHashMap<>();
ApiParam apiParam = this.getApiParam(request);
if (apiParam != null) {
// 全局请求id,方便追踪定位
responseData.put("request_id", apiParam.fetchRequestId());
serviceData.put("request_id", apiParam.fetchRequestId());
}
responseData.put(GATEWAY_CODE_NAME, errorMeta.getCode());
responseData.put(GATEWAY_MSG_NAME, errorMeta.getError(getLocale(request)).getMsg());
return responseData;
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);
return serviceData;
}
@ -183,7 +187,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
return serviceResult;
}
public String merge(T exchange, JSONObject responseData) {
public String merge(T exchange, Map<String, Object> responseData) {
JSONObject finalData = new JSONObject(true);
ApiParam params = this.getApiParam(exchange);
if (params == null) {

Loading…
Cancel
Save