paramExecutor;
@@ -39,19 +48,19 @@ public class IndexController {
* 请求入口
*
* @apiNote 参数描述
-
- 参数 类型 是否必填 最大长度 描述 示例值
- app_id String 是 32 平台分配给开发者的应用ID 2014072300007148
- method String 是 128 接口名称 alipay.trade.fastpay.refund.query
- format String 否 40 仅支持JSON JSON
- charset String 是 10 请求使用的编码格式,如utf-8,gbk,gb2312等 utf-8
- sign_type String 是 10 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2 RSA2
- sign String 是 344 商户请求参数的签名串,详见签名 详见示例
- timestamp String 是 19 发送请求的时间,格式"yyyy-MM-dd HH:mm:ss" 2014-07-24 03:07:50
- version String 是 3 调用的接口版本,固定为:1.0 1.0
- app_auth_token String 否 40 详见应用授权概述
- biz_content String 是 请求参数的集合,最大长度不限,除公共参数外所有请求参数都必须放在这个参数中传递,具体参照各产品快速接入文档
-
+ *
+ * 参数 类型 是否必填 最大长度 描述 示例值
+ * app_id String 是 32 平台分配给开发者的应用ID 2014072300007148
+ * method String 是 128 接口名称 alipay.trade.fastpay.refund.query
+ * format String 否 40 仅支持JSON JSON
+ * charset String 是 10 请求使用的编码格式,如utf-8,gbk,gb2312等 utf-8
+ * sign_type String 是 10 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2 RSA2
+ * sign String 是 344 商户请求参数的签名串,详见签名 详见示例
+ * timestamp String 是 19 发送请求的时间,格式"yyyy-MM-dd HH:mm:ss" 2014-07-24 03:07:50
+ * version String 是 3 调用的接口版本,固定为:1.0 1.0
+ * app_auth_token String 否 40 详见应用授权概述
+ * biz_content String 是 请求参数的集合,最大长度不限,除公共参数外所有请求参数都必须放在这个参数中传递,具体参照各产品快速接入文档
+ *
*/
@RequestMapping(value = "${gateway.path:/api}", method = {RequestMethod.GET, RequestMethod.POST})
public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
@@ -60,4 +69,46 @@ public class IndexController {
paramExecutor.write(apiRequestContext, apiResponse, response);
}
+ /**
+ * restful请求
+ *
+ * @param request
+ * @param response
+ * @param path 路径,对应@Open.value
+ * @param version 版本号
+ * @throws IOException
+ */
+ @RequestMapping(value = "${gateway.rest}/{path}/{version}")
+ public void rest0(
+ HttpServletRequest request,
+ HttpServletResponse response,
+ @PathVariable String path,
+ @PathVariable String version
+ ) throws IOException {
+ ApiRequestContext apiRequestContext = paramExecutor.build(request);
+ ApiRequest apiRequest = apiRequestContext.getApiRequest();
+ apiRequest.setMethod(path);
+ apiRequest.setVersion(version);
+ apiRequestContext.setIsRest(true);
+ Response apiResponse = restRouteService.route(apiRequestContext);
+ paramExecutor.write(apiRequestContext, apiResponse, response);
+ }
+
+ /**
+ * restful请求
+ *
+ * @param request
+ * @param response
+ * @param path 路径,对应@Open.value
+ * @throws IOException
+ */
+ @RequestMapping(value = "${gateway.rest}/{path}")
+ public void rest(
+ HttpServletRequest request,
+ HttpServletResponse response,
+ @PathVariable String path
+ ) throws IOException {
+ this.rest0(request, response, path, SopConstants.DEFAULT_VERSION);
+ }
+
}
diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/exception/impl/ExceptionExecutorImpl.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/exception/impl/ExceptionExecutorImpl.java
index e13b90d2..a376124d 100755
--- a/sop-gateway/src/main/java/com/gitee/sop/gateway/exception/impl/ExceptionExecutorImpl.java
+++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/exception/impl/ExceptionExecutorImpl.java
@@ -32,7 +32,7 @@ public class ExceptionExecutorImpl implements ExceptionExecutor {
String exceptionClass = genericException.getExceptionClass();
if (exceptionClass.contains(CONSTRAINT_VIOLATION_EXCEPTION)) {
String exceptionMessage = genericException.getExceptionMessage();
- // 参数校验:Failed to validate service: com.gitee.sop.storyweb.open.StoryService, method: save, cause: [ConstraintViolationImpl{interpolatedMessage='故事名称必填', propertyPath=storyName, rootBeanClass=class com.gitee.sop.storyweb.open.req.StorySaveDTO, messageTemplate='故事名称必填'}]
+ // 参数校验:Failed to validate service: com.gitee.sop.storyweb.open.StoryService, method: save, cause: [ConstraintViolationImpl{interpolatedMessage='故事名称必填', propertyPath=productName, rootBeanClass=class com.gitee.sop.storyweb.open.req.StorySaveDTO, messageTemplate='故事名称必填'}]
Set msgs = findErrorMsg(exceptionMessage);
return ApiResponse.error(ErrorEnum.ISV_ERROR_PARAMETER, apiRequestContext.getLocale(), String.join(",", msgs));
}
diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/request/ApiRequestContext.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/request/ApiRequestContext.java
index 21e4eed6..c1af6022 100755
--- a/sop-gateway/src/main/java/com/gitee/sop/gateway/request/ApiRequestContext.java
+++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/request/ApiRequestContext.java
@@ -1,5 +1,6 @@
package com.gitee.sop.gateway.request;
+import com.alibaba.fastjson2.JSONObject;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@@ -33,4 +34,20 @@ public class ApiRequestContext {
* 上传文件
*/
private UploadContext uploadContext;
+
+ /**
+ * 标签
+ */
+ private String tag;
+
+ /**
+ * 是否需要签名校验
+ */
+ @Builder.Default
+ private Boolean isRest = false;
+
+ /**
+ * 原始参数
+ */
+ private JSONObject rawParams;
}
diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/request/ApiRequestContextFactory.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/request/ApiRequestContextFactory.java
deleted file mode 100755
index 22ca94a6..00000000
--- a/sop-gateway/src/main/java/com/gitee/sop/gateway/request/ApiRequestContextFactory.java
+++ /dev/null
@@ -1,74 +0,0 @@
-package com.gitee.sop.gateway.request;
-
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONObject;
-import com.gitee.sop.gateway.common.SopConstants;
-import com.gitee.sop.gateway.config.ApiConfig;
-import com.gitee.sop.gateway.util.RequestUtil;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringUtils;
-
-import javax.servlet.http.HttpServletRequest;
-import java.io.IOException;
-import java.util.UUID;
-
-/**
- * @author 六如
- */
-@Slf4j
-public class ApiRequestContextFactory {
-
- private static final String CONTENT_TYPE = "content-type";
- private static final String JSON_NAME = "json";
- private static final String TEXT_NAME = "text";
- private static final String MULTIPART = "multipart";
- public static final String FORM = "form";
-
- public static ApiRequestContext build(HttpServletRequest request, ApiConfig apiConfig) {
- // get请求可能返回null
- String contentType = request.getHeader(CONTENT_TYPE);
- if (contentType == null) {
- contentType = "";
- }
- ApiRequest apiRequest = new ApiRequest();
- String ip = RequestUtil.getIP(request);
- byte[] body;
- try {
- body = IOUtils.toByteArray(request.getInputStream());
- } catch (IOException e) {
- log.error("获取请求体失败", e);
- body = new byte[0];
- }
- JSONObject params = null;
- UploadContext uploadContext = null;
- String contentTypeStr = contentType.toLowerCase();
- // 如果是json方式提交
- if (StringUtils.containsAny(contentTypeStr, JSON_NAME, TEXT_NAME)) {
- params = JSON.parseObject(body);
- } else if (StringUtils.containsIgnoreCase(contentTypeStr, MULTIPART)) {
- // 如果是文件上传请求
- RequestUtil.UploadInfo uploadInfo = RequestUtil.getUploadInfo(request);
- params = uploadInfo.getApiParam();
- uploadContext = uploadInfo.getUploadContext();
- } else if (StringUtils.containsIgnoreCase(contentTypeStr, FORM)) {
- // APPLICATION_FORM_URLENCODED请求
- params = RequestUtil.parseQuerystring(new String(body, SopConstants.CHARSET_UTF8));
- } else {
- // get请求,参数跟在url后面
- params = RequestUtil.parseParameterMap(request.getParameterMap());
- }
- if (params != null) {
- apiRequest = params.toJavaObject(ApiRequest.class);
- }
-
- return ApiRequestContext.builder()
- .apiRequest(apiRequest)
- .locale(request.getLocale())
- .ip(ip)
- .uploadContext(uploadContext)
- .traceId(UUID.randomUUID().toString().replace("-", ""))
- .build();
- }
-
-}
diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/service/Serde.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/service/Serde.java
index d1e765fd..4f1e9262 100755
--- a/sop-gateway/src/main/java/com/gitee/sop/gateway/service/Serde.java
+++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/service/Serde.java
@@ -18,6 +18,9 @@ public interface Serde {
Map parseJson(String json);
default JSONObject parseObject(String json) {
+ if (json == null) {
+ return null;
+ }
Map jsonObj = parseJson(json);
return jsonObj instanceof JSONObject ? (JSONObject) jsonObj : new JSONObject(jsonObj);
}
diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/service/impl/ParamExecutorImpl.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/service/impl/ParamExecutorImpl.java
index 47705b43..75dd21a3 100755
--- a/sop-gateway/src/main/java/com/gitee/sop/gateway/service/impl/ParamExecutorImpl.java
+++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/service/impl/ParamExecutorImpl.java
@@ -82,31 +82,38 @@ public class ParamExecutorImpl implements ParamExecutor paramInfoList = JSON.parseArray(paramInfo, ParamInfoDTO.class);
+ OpenContext openRequest = buildOpenContext(apiRequestContext);
+ RpcContext.getClientAttachment().setAttachment(SopConstants.OPEN_CONTEXT, JSON.toJSONString(openRequest));
return genericServiceInvoker.invoke(
apiInfo.getInterfaceClassName(),
apiInfo.getMethodName(),
buildParamType(paramInfoList),
- buildInvokeParam(apiRequestContext, paramInfoList)
+ buildInvokeParam(apiRequestContext, paramInfoList, openRequest)
);
}
@@ -121,13 +138,14 @@ public class RouteServiceImpl implements RouteService {
.toArray(String[]::new);
}
- protected Object[] buildInvokeParam(ApiRequestContext apiRequestContext, List paramInfoList) {
+ protected Object[] buildInvokeParam(
+ ApiRequestContext apiRequestContext,
+ List paramInfoList,
+ OpenContext openRequest) {
if (ObjectUtils.isEmpty(paramInfoList)) {
return new Object[0];
}
- ApiRequest apiRequest = apiRequestContext.getApiRequest();
- String bizContent = apiRequest.getBizContent();
- JSONObject jsonObject = serde.parseObject(bizContent);
+ JSONObject jsonObject = getParamObject(apiRequestContext);
List