From a5dbf715134106a5c3b5f394dd93c5274840f113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AD=E5=A6=82?= <8775@163.com> Date: Mon, 3 Feb 2025 11:31:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EOpenGroup=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../examplerest/rest/GoodsController.java | 2 + .../gateway/controller/IndexController.java | 44 +++++++++---------- .../sop/gateway/response/ApiResponse.java | 4 +- .../resources/i18n/open/code_zh_CN.properties | 2 +- .../i18n/open/error_zh_CN.properties | 6 +-- .../sop/support/annotation/OpenGroup.java | 26 +++++++++++ .../sop/support/register/ApiRegister.java | 12 +++-- 7 files changed, 64 insertions(+), 32 deletions(-) create mode 100755 sop-support/sop-service-support/src/main/java/com/gitee/sop/support/annotation/OpenGroup.java diff --git a/sop-example/example-rest/src/main/java/com/sop/example/rest/examplerest/rest/GoodsController.java b/sop-example/example-rest/src/main/java/com/sop/example/rest/examplerest/rest/GoodsController.java index 611a8a15..6b42da55 100644 --- a/sop-example/example-rest/src/main/java/com/sop/example/rest/examplerest/rest/GoodsController.java +++ b/sop-example/example-rest/src/main/java/com/sop/example/rest/examplerest/rest/GoodsController.java @@ -1,6 +1,7 @@ package com.sop.example.rest.examplerest.rest; import com.gitee.sop.support.annotation.Open; +import com.gitee.sop.support.annotation.OpenGroup; import com.sop.example.rest.examplerest.rest.vo.GoodsVO; import javax.validation.constraints.NotNull; @@ -8,6 +9,7 @@ import javax.validation.constraints.NotNull; /** * @author 六如 */ +@OpenGroup("goods") public interface GoodsController { @Open("/getGoodsById") diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/IndexController.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/IndexController.java index 196bf69b..63fe165f 100755 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/IndexController.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/IndexController.java @@ -8,11 +8,14 @@ import com.gitee.sop.gateway.service.ParamExecutor; import com.gitee.sop.gateway.service.RouteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -34,6 +37,9 @@ public class IndexController { @Qualifier("restRouteService") private RouteService restRouteService; + @Value("${gateway.rest}") + private String restPrefix; + @Autowired private ParamExecutor paramExecutor; @@ -71,44 +77,36 @@ public class IndexController { /** * restful请求 - * + *

+ * 格式:http://<网关host>:<网关port>/rest/[?v=<版本号>] + *

* @param request * @param response - * @param path 路径,对应@Open.value - * @param version 版本号 + * @param v 版本号 * @throws IOException */ - @RequestMapping(value = "${gateway.rest}/{path}/{version}") + @RequestMapping(value = "${gateway.rest}/**") public void rest0( HttpServletRequest request, HttpServletResponse response, - @PathVariable String path, - @PathVariable String version + @RequestParam(value = "v", required = false, defaultValue = "1.0") String v ) throws IOException { ApiRequestContext apiRequestContext = paramExecutor.build(request); ApiRequest apiRequest = apiRequestContext.getApiRequest(); - apiRequest.setMethod(path); - apiRequest.setVersion(version); + String apiName = getApiName(request); + apiRequest.setMethod(apiName); + apiRequest.setVersion(v); 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); + private String getApiName(HttpServletRequest request) { + String requestURI = request.getRequestURI(); + String apiName = requestURI.substring(restPrefix.length()); + return StringUtils.trimLeadingCharacter(apiName, '/'); } + + } diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/response/ApiResponse.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/response/ApiResponse.java index c71e9882..83839765 100755 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/response/ApiResponse.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/response/ApiResponse.java @@ -16,7 +16,7 @@ import java.util.Locale; * 50 * Remote service error * isv.invalid-parameter - * 非法参数 + * 参数错误 * * 成功情况: * @@ -32,7 +32,7 @@ import java.util.Locale; * "code":"50", * "msg":"Remote service error", * "sub_code":"isv.invalid-parameter", - * "sub_msg":"非法参数" + * "sub_msg":"参数错误" * } * 成功情况: * { diff --git a/sop-gateway/src/main/resources/i18n/open/code_zh_CN.properties b/sop-gateway/src/main/resources/i18n/open/code_zh_CN.properties index 6737554a..d1b922d8 100755 --- a/sop-gateway/src/main/resources/i18n/open/code_zh_CN.properties +++ b/sop-gateway/src/main/resources/i18n/open/code_zh_CN.properties @@ -1,7 +1,7 @@ 0=Success 20001=\u6388\u6743\u6743\u9650\u4E0D\u8DB3 40001=\u7F3A\u5C11\u5FC5\u9009\u53C2\u6570 -40002=\u975E\u6CD5\u7684\u53C2\u6570 +40002=\u53C2\u6570\u9519\u8BEF 50003=\u4E1A\u52A1\u5904\u7406\u5931\u8D25 40006=\u6743\u9650\u4E0D\u8DB3 99999=\u670D\u52A1\u4E0D\u53EF\u7528 diff --git a/sop-gateway/src/main/resources/i18n/open/error_zh_CN.properties b/sop-gateway/src/main/resources/i18n/open/error_zh_CN.properties index 5a8c1dba..1d68c862 100755 --- a/sop-gateway/src/main/resources/i18n/open/error_zh_CN.properties +++ b/sop-gateway/src/main/resources/i18n/open/error_zh_CN.properties @@ -2,7 +2,7 @@ isp.unknown-error=\u670D\u52A1\u6682\u4E0D\u53EF\u7528 isp.service-unknown-error=\u670D\u52A1\u4E0D\u53EF\u7528 isp.service-not-available=\u670D\u52A1\u6682\u4E0D\u53EF\u7528 isp.gateway-response-timeout=\u7F51\u5173\u54CD\u5E94\u8D85\u65F6 -isp.biz-error=\u4e1a\u52a1\u5f02\u5e38 +isp.biz-error=\u4E1A\u52A1\u5F02\u5E38 isv.service-busy=\u670D\u52A1\u5668\u5FD9 aop.invalid-auth-token=\u65E0\u6548\u7684\u8BBF\u95EE\u4EE4\u724C @@ -22,7 +22,7 @@ isv.missing-version=\u7F3A\u5C11\u7248\u672C\u53C2\u6570 isv.decryption-error-missing-encrypt-type=\u89E3\u5BC6\u51FA\u9519, \u672A\u6307\u5B9A\u52A0\u5BC6\u7B97\u6CD5 isv.invalid-parameter=\u53C2\u6570\u65E0\u6548 -isv.error-parameter=\u53c2\u6570\u4e0d\u6b63\u786e +isv.error-parameter=\u53C2\u6570\u4E0D\u6B63\u786E isv.upload-fail=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25 isv.invalid-file-extension=\u6587\u4EF6\u6269\u5C55\u540D\u65E0\u6548 isv.invalid-file-size={0}\u6587\u4EF6\u5927\u5C0F\u65E0\u6548\uFF0C\u5355\u6587\u4EF6\u4E0D\u5F97\u8D85\u8FC7{1} @@ -33,7 +33,7 @@ isv.invalid-signature=\u65E0\u6548\u7B7E\u540D isv.invalid-encrypt-type=\u65E0\u6548\u7684\u52A0\u5BC6\u7C7B\u578B isv.invalid-encrypt=\u89E3\u5BC6\u5F02\u5E38 isv.invalid-app-id=\u65E0\u6548\u7684 appId \u53C2\u6570 -isv.invalid-timestamp=\u975E\u6CD5\u7684\u65F6\u95F4\u6233\u53C2\u6570 +isv.invalid-timestamp=\u9519\u8BEF\u7684\u65F6\u95F4\u6233\u53C2\u6570 isv.invalid-charset=\u5B57\u7B26\u96C6\u9519\u8BEF isv.invalid-digest=\u6458\u8981\u9519\u8BEF isv.decryption-error-not-valid-encrypt-type=\u89E3\u5BC6\u51FA\u9519\uFF0C\u4E0D\u652F\u6301\u7684\u52A0\u5BC6\u7B97\u6CD5 diff --git a/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/annotation/OpenGroup.java b/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/annotation/OpenGroup.java new file mode 100755 index 00000000..848aedcf --- /dev/null +++ b/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/annotation/OpenGroup.java @@ -0,0 +1,26 @@ +package com.gitee.sop.support.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 分组 + * + * @author 六如 + */ +@Inherited +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface OpenGroup { + + /** + * 分组名称 + */ + String value(); + +} diff --git a/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/register/ApiRegister.java b/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/register/ApiRegister.java index 672aeb5a..53120c62 100755 --- a/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/register/ApiRegister.java +++ b/sop-support/sop-service-support/src/main/java/com/gitee/sop/support/register/ApiRegister.java @@ -2,6 +2,7 @@ package com.gitee.sop.support.register; import com.alibaba.fastjson2.JSON; import com.gitee.sop.support.annotation.Open; +import com.gitee.sop.support.annotation.OpenGroup; import com.gitee.sop.support.message.OpenMessageFactory; import com.gitee.sop.support.service.ApiRegisterService; import com.gitee.sop.support.service.dto.RegisterDTO; @@ -114,7 +115,7 @@ public class ApiRegister { List paramInfos = buildParamInfo(method); RegisterDTO registerDTO = new RegisterDTO(); registerDTO.setApplication(appName); - registerDTO.setApiName(getApiName(open)); + registerDTO.setApiName(getApiName(interfaceClass, open)); registerDTO.setApiVersion(open.version()); registerDTO.setInterfaceClassName(interfaceClass.getName()); registerDTO.setMethodName(method.getName()); @@ -133,8 +134,13 @@ public class ApiRegister { } } - protected String getApiName(Open open) { - return StringUtils.trimLeadingCharacter(open.value(), '/'); + protected String getApiName(Class interfaceClass, Open open) { + String apiName = StringUtils.trimLeadingCharacter(open.value(), '/'); + OpenGroup openGroup = interfaceClass.getAnnotation(OpenGroup.class); + if (openGroup != null) { + apiName = openGroup.value() + "/" + apiName; + } + return StringUtils.trimLeadingCharacter(apiName, '/'); } private List buildParamInfo(Method method) {