847850277 commented on code in PR #4270:
URL: https://github.com/apache/shenyu/pull/4270#discussion_r1056907317
##########
shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/main/java/org/apache/shenyu/client/springcloud/init/SpringCloudClientEventListener.java:
##########
@@ -92,6 +106,123 @@ public SpringCloudClientEventListener(final
PropertiesConfig clientConfig,
mappingAnnotation.add(RequestMapping.class);
}
+ /**
+ * buildApiDocDTO.
+ *
+ * @param clazz clazz
+ * @param method method
+ * @return ApiDocRegisterDTO
+ */
+ @Override
+ protected List<ApiDocRegisterDTO> buildApiDocDTO(final Class<?> clazz,
final Method method) {
+ final String contextPath = getContextPath();
+ String apiDesc =
Stream.of(method.getDeclaredAnnotations()).filter(item -> item instanceof
ApiDoc).findAny().map(item -> {
+ ApiDoc apiDoc = (ApiDoc) item;
+ return apiDoc.desc();
+ }).orElse("");
+ String superPath = buildApiSuperPath(clazz,
AnnotatedElementUtils.findMergedAnnotation(clazz, getAnnotationType()));
+ if (superPath.indexOf("*") > 0) {
+ superPath = superPath.substring(0, superPath.lastIndexOf("/"));
+ }
+ RequestMapping requestMapping =
AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
+ if (Objects.nonNull(requestMapping)) {
+ List<ApiDocRegisterDTO> list = Lists.newArrayList();
+ String produce = requestMapping.produces().length == 0 ?
ShenyuClientConstants.MEDIA_TYPE_ALL_VALUE : String.join(",",
requestMapping.produces());
+ String consume = requestMapping.consumes().length == 0 ?
ShenyuClientConstants.MEDIA_TYPE_ALL_VALUE : String.join(",",
requestMapping.consumes());
+ String[] values = requestMapping.value();
+ for (String value : values) {
+ String apiPath = contextPath + superPath + value;
+ RequestMethod[] requestMethods = requestMapping.method();
+ if (requestMethods.length == 0) {
+ requestMethods = RequestMethod.values();
+ }
+ for (RequestMethod requestMethod : requestMethods) {
+ ApiDocRegisterDTO build = ApiDocRegisterDTO.builder()
+ .consume(consume)
+ .produce(produce)
+
.httpMethod(ApiHttpMethodEnum.getValueByName(String.valueOf(requestMethod)))
+ .contextPath(contextPath)
+ .ext("{}")
+ .document("{}")
+ .version("v0.01")
+ .rpcType(RpcTypeEnum.SPRING_CLOUD.getName())
+ .apiDesc(apiDesc)
+ .apiPath(apiPath)
+ .apiSource(1)
+ .state(1)
+ .apiOwner("admin")
+ .eventType(EventType.REGISTER)
+ .build();
+ list.add(build);
+ }
+ }
+ return list;
+ } else {
+ return apiDocRegisterDTOListByDeclaredAnnotations(contextPath,
superPath, method, apiDesc);
+ }
+ }
+
+ private List<ApiDocRegisterDTO>
apiDocRegisterDTOListByDeclaredAnnotations(final String contextPath, final
String superPath, final Method method, final String apiDesc) {
+ List<ApiDocRegisterDTO> list = Lists.newArrayList();
+ Map<ApiHttpMethodEnum, Triple<String[], String[], String[]>>
methodConsumeProduceValueMap = httpMethodConsumeProduceValueMap(method);
+ methodConsumeProduceValueMap.forEach((k, v) -> {
+ String[] values = v.getRight();
+ String consume = v.getLeft().length == 0 ?
ShenyuClientConstants.MEDIA_TYPE_ALL_VALUE : String.join(",", v.getLeft());
+ String produce = v.getMiddle().length == 0 ?
ShenyuClientConstants.MEDIA_TYPE_ALL_VALUE : String.join(",", v.getMiddle());
+ for (String value : values) {
+ String apiPath = contextPath + superPath + value;
+ ApiDocRegisterDTO build = ApiDocRegisterDTO.builder()
+ .consume(consume)
+ .produce(produce)
+ .httpMethod(k.getValue())
+ .contextPath(contextPath)
+ .ext("{}")
+ .document("{}")
+ .version("v0.01")
+ .rpcType(RpcTypeEnum.SPRING_CLOUD.getName())
+ .apiDesc(apiDesc)
+ .apiPath(apiPath)
+ .apiSource(1)
+ .state(1)
+ .apiOwner("admin")
+ .eventType(EventType.REGISTER)
+ .build();
+ list.add(build);
+ }
+ });
+ return list;
+ }
+
+ private Map<ApiHttpMethodEnum, Triple<String[], String[], String[]>>
httpMethodConsumeProduceValueMap(final Method method) {
+ Map<ApiHttpMethodEnum, Triple<String[], String[], String[]>> map =
Maps.newHashMap();
+ GetMapping getMapping =
AnnotatedElementUtils.findMergedAnnotation(method, GetMapping.class);
+ if (Objects.nonNull(getMapping)) {
+ map.put(ApiHttpMethodEnum.GET, Triple.of(getMapping.consumes(),
getMapping.produces(), getMapping.value()));
+ return map;
+ }
Review Comment:
in SpringMvcClientEventListener or SpringCloudClientEventListener. this code
in line 122.
`RequestMapping` or `GetMapping` They are different annotation
```java
RequestMapping requestMapping =
AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (Objects.nonNull(requestMapping)) {
List<ApiDocRegisterDTO> list = Lists.newArrayList();
String produce = requestMapping.produces().length == 0 ?
ShenyuClientConstants.MEDIA_TYPE_ALL_VALUE : String.join(",",
requestMapping.produces());
String consume = requestMapping.consumes().length == 0 ?
ShenyuClientConstants.MEDIA_TYPE_ALL_VALUE : String.join(",",
requestMapping.consumes());
String[] values = requestMapping.value();
for (String value : values) {
String apiPath = contextPath + superPath + value;
RequestMethod[] requestMethods = requestMapping.method();
if (requestMethods.length == 0) {
requestMethods = RequestMethod.values();
}
for (RequestMethod requestMethod : requestMethods) {
ApiDocRegisterDTO build = ApiDocRegisterDTO.builder()
.consume(consume)
.produce(produce)
.httpMethod(ApiHttpMethodEnum.getValueByName(String.valueOf(requestMethod)))
.contextPath(contextPath)
.ext("{}")
.document("{}")
.version("v0.01")
.rpcType(RpcTypeEnum.HTTP.getName())
.apiDesc(apiDesc)
.apiPath(apiPath)
.apiSource(ApiSourceEnum.ANNOTATION_GENERATION.getValue())
.state(ApiStateEnum.PUBLISHED.getState())
.apiOwner("admin")
.eventType(EventType.REGISTER)
.build();
list.add(build);
}
}
return list;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]