From d6eef1218e953fc9169654884e95f22b816abb0c Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 26 Nov 2019 16:51:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E6=A1=A3=E6=98=BE?= =?UTF-8?q?=E7=A4=BABUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 5 +++++ .../sop/servercommon/bean/ServiceApiInfo.java | 20 ++++++++++++++++++- .../servercommon/manager/ApiMetaBuilder.java | 17 +++++++++++----- .../mapping/ApiMappingHandlerMapping.java | 13 ++++++++++++ .../ApiMappingStringValueResolver.java | 7 +------ .../TraditionalWebappController.java | 4 ++++ .../manager/SwaggerDocParser.java | 11 +++++++++- 7 files changed, 64 insertions(+), 13 deletions(-) diff --git a/changelog.md b/changelog.md index edb75ef8..fca5cf17 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # changelog +## 2.5.6 + +- 优化文档显示 +- 修复路由拉取接口重复BUG + ## 2.5.5 - 优化文档显示 diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java index 7998d941..85d1f9a3 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java @@ -2,8 +2,11 @@ package com.gitee.sop.servercommon.bean; import com.gitee.sop.servercommon.route.RouteDefinition; import lombok.Data; +import lombok.Getter; +import lombok.Setter; import java.util.List; +import java.util.Objects; /** * @author tanghc @@ -14,7 +17,8 @@ public class ServiceApiInfo { private List apis; private List routeDefinitionList; - @Data + @Getter + @Setter public static class ApiMeta { /** 接口名 */ private String name; @@ -45,5 +49,19 @@ public class ServiceApiInfo { public String fetchNameVersion() { return this.name + this.version; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ApiMeta apiMeta = (ApiMeta) o; + return name.equals(apiMeta.name) && + Objects.equals(version, apiMeta.version); + } + + @Override + public int hashCode() { + return Objects.hash(name, version); + } } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ApiMetaBuilder.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ApiMetaBuilder.java index 5c1b8692..8f3ecb17 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ApiMetaBuilder.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ApiMetaBuilder.java @@ -16,6 +16,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -50,6 +51,7 @@ public class ApiMetaBuilder { Set requestMappingInfos = handlerMethods.keySet(); List store = new ArrayList<>(); List apis = new ArrayList<>(requestMappingInfos.size()); + Set restfulApis = new HashSet<>(requestMappingInfos.size()); for (Map.Entry handlerMethodEntry : handlerMethods.entrySet()) { ServiceApiInfo.ApiMeta apiMeta = this.buildApiMeta(handlerMethodEntry); @@ -57,13 +59,18 @@ public class ApiMetaBuilder { continue; } String key = apiMeta.fetchNameVersion(); - if (!apiMeta.isOriginalMapping() && store.contains(key)) { - throw new IllegalArgumentException("重复申明接口,请检查path和version,path:" + apiMeta.getPath() + ", version:" + apiMeta.getVersion()); + if (apiMeta.isOriginalMapping()) { + restfulApis.add(apiMeta); } else { - store.add(key); + if (store.contains(key)) { + throw new IllegalArgumentException("重复申明接口,请检查path和version,path:" + apiMeta.getPath() + ", version:" + apiMeta.getVersion()); + } else { + store.add(key); + } + apis.add(apiMeta); } - apis.add(apiMeta); } + apis.addAll(restfulApis); return apis; } @@ -93,11 +100,11 @@ public class ApiMetaBuilder { if (!ServiceContext.getCurrentContext().getBoolean(ServiceContext.RESTFUL_KEY, false)) { return null; } - // 如果是restful服务 String path = patterns.iterator().next(); if (path.contains("$") || isIgnorePattern(path)) { return null; } + // 如果是restful服务 String name = path; String prefix = EnvironmentKeys.SOP_RESTFUL_PREFIX.getValue(); if (StringUtils.isEmpty(prefix)) { diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java index 8506f015..506704b6 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java @@ -17,6 +17,19 @@ import java.lang.reflect.Method; */ public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping implements PriorityOrdered { + private static StringValueResolver stringValueResolver = new ApiMappingStringValueResolver(); + + @Override + protected RequestMappingInfo getMappingForMethod(Method method, Class handlerType) { + ApiMapping apiMapping = method.getAnnotation(ApiMapping.class); + StringValueResolver valueResolver = null; + if (apiMapping != null) { + valueResolver = stringValueResolver; + } + this.setEmbeddedValueResolver(valueResolver); + return super.getMappingForMethod(method, handlerType); + } + @Override protected RequestCondition getCustomMethodCondition(Method method) { method.setAccessible(true); diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingStringValueResolver.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingStringValueResolver.java index d17ce61a..da448bab 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingStringValueResolver.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingStringValueResolver.java @@ -7,13 +7,8 @@ import org.springframework.util.StringValueResolver; */ public class ApiMappingStringValueResolver implements StringValueResolver { - private static final String END_CHAR = "/"; - @Override public String resolveStringValue(String strVal) { - if (strVal != null && !strVal.endsWith(END_CHAR)) { - return strVal + END_CHAR; - } - return null; + return strVal; } } diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java index 64752165..7cf77b49 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java @@ -1,6 +1,8 @@ package com.gitee.sop.storyweb.controller; import com.gitee.sop.servercommon.util.UploadUtil; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; import lombok.Data; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -20,11 +22,13 @@ import java.util.Collection; */ @RestController @RequestMapping("food") +@Api(tags = "食物接口") public class TraditionalWebappController { // http://localhost:8081/rest/food/getFoodById?id=1 网关入口 // http://localhost:2222/food/getFoodById/?id=12 本地入口 + @ApiOperation(value="获取食物", notes = "根据id获取食物") @RequestMapping(value = "getFoodById", method = RequestMethod.GET) public Food getFoodById(Integer id) { Food food = new Food(); diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java index bb822cbf..ff845789 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java @@ -8,6 +8,7 @@ import com.gitee.sop.websiteserver.bean.DocModule; import com.gitee.sop.websiteserver.bean.DocParameter; import com.gitee.sop.websiteserver.bean.DocParserContext; import com.google.common.collect.Sets; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.BeanUtils; import org.springframework.util.CollectionUtils; @@ -48,6 +49,9 @@ public class SwaggerDocParser implements DocParser { String method = first.get(); JSONObject docInfo = pathInfo.getJSONObject(method); DocItem docItem = buildDocItem(docInfo, docRoot); + if (docItem == null) { + continue; + } if (docItem.isUploadRequest()) { docItem.setHttpMethodList(Sets.newHashSet("post")); } else { @@ -103,8 +107,13 @@ public class SwaggerDocParser implements DocParser { } protected DocItem buildDocItem(JSONObject docInfo, JSONObject docRoot) { + String apiName = docInfo.getString("sop_name"); + // 非开放接口 + if (StringUtils.isBlank(apiName)) { + return null; + } DocItem docItem = new DocItem(); - docItem.setName(docInfo.getString("sop_name")); + docItem.setName(apiName); docItem.setVersion(docInfo.getString("sop_version")); docItem.setSummary(docInfo.getString("summary")); docItem.setDescription(docInfo.getString("description"));