新增OpenGroup注解

This commit is contained in:
六如
2025-02-03 11:31:29 +08:00
parent aa8f044c70
commit a5dbf71513
7 changed files with 64 additions and 32 deletions

View File

@@ -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")

View File

@@ -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<HttpServletRequest, HttpServletResponse> paramExecutor;
@@ -71,44 +77,36 @@ public class IndexController {
/**
* restful请求
*
* <p>
* 格式http://<网关host>:<网关port>/rest/<value>[?v=<版本号>]
* </p>
* @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, '/');
}
}

View File

@@ -16,7 +16,7 @@ import java.util.Locale;
* <code>50</code>
* <msg>Remote service error</msg>
* <sub_code>isv.invalid-parameter</sub_code>
* <sub_msg>非法参数</sub_msg>
* <sub_msg>参数错误</sub_msg>
* </response>
* 成功情况:
* <response>
@@ -32,7 +32,7 @@ import java.util.Locale;
* "code":"50",
* "msg":"Remote service error",
* "sub_code":"isv.invalid-parameter",
* "sub_msg":"非法参数"
* "sub_msg":"参数错误"
* }
* 成功情况:
* {

View File

@@ -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

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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<ParamInfo> 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<ParamInfo> buildParamInfo(Method method) {