修复文档显示BUG

pull/1/head
tanghc 5 years ago
parent 2e88574284
commit d6eef1218e
  1. 5
      changelog.md
  2. 20
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceApiInfo.java
  3. 17
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ApiMetaBuilder.java
  4. 13
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingHandlerMapping.java
  5. 7
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingStringValueResolver.java
  6. 4
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java
  7. 11
      sop-website/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java

@ -1,5 +1,10 @@
# changelog # changelog
## 2.5.6
- 优化文档显示
- 修复路由拉取接口重复BUG
## 2.5.5 ## 2.5.5
- 优化文档显示 - 优化文档显示

@ -2,8 +2,11 @@ package com.gitee.sop.servercommon.bean;
import com.gitee.sop.servercommon.route.RouteDefinition; import com.gitee.sop.servercommon.route.RouteDefinition;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* @author tanghc * @author tanghc
@ -14,7 +17,8 @@ public class ServiceApiInfo {
private List<ApiMeta> apis; private List<ApiMeta> apis;
private List<RouteDefinition> routeDefinitionList; private List<RouteDefinition> routeDefinitionList;
@Data @Getter
@Setter
public static class ApiMeta { public static class ApiMeta {
/** 接口名 */ /** 接口名 */
private String name; private String name;
@ -45,5 +49,19 @@ public class ServiceApiInfo {
public String fetchNameVersion() { public String fetchNameVersion() {
return this.name + this.version; 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);
}
} }
} }

@ -16,6 +16,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -50,6 +51,7 @@ public class ApiMetaBuilder {
Set<RequestMappingInfo> requestMappingInfos = handlerMethods.keySet(); Set<RequestMappingInfo> requestMappingInfos = handlerMethods.keySet();
List<String> store = new ArrayList<>(); List<String> store = new ArrayList<>();
List<ServiceApiInfo.ApiMeta> apis = new ArrayList<>(requestMappingInfos.size()); List<ServiceApiInfo.ApiMeta> apis = new ArrayList<>(requestMappingInfos.size());
Set<ServiceApiInfo.ApiMeta> restfulApis = new HashSet<>(requestMappingInfos.size());
for (Map.Entry<RequestMappingInfo, HandlerMethod> handlerMethodEntry : handlerMethods.entrySet()) { for (Map.Entry<RequestMappingInfo, HandlerMethod> handlerMethodEntry : handlerMethods.entrySet()) {
ServiceApiInfo.ApiMeta apiMeta = this.buildApiMeta(handlerMethodEntry); ServiceApiInfo.ApiMeta apiMeta = this.buildApiMeta(handlerMethodEntry);
@ -57,13 +59,18 @@ public class ApiMetaBuilder {
continue; continue;
} }
String key = apiMeta.fetchNameVersion(); String key = apiMeta.fetchNameVersion();
if (!apiMeta.isOriginalMapping() && store.contains(key)) { if (apiMeta.isOriginalMapping()) {
throw new IllegalArgumentException("重复申明接口,请检查path和version,path:" + apiMeta.getPath() + ", version:" + apiMeta.getVersion()); restfulApis.add(apiMeta);
} else { } 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; return apis;
} }
@ -93,11 +100,11 @@ public class ApiMetaBuilder {
if (!ServiceContext.getCurrentContext().getBoolean(ServiceContext.RESTFUL_KEY, false)) { if (!ServiceContext.getCurrentContext().getBoolean(ServiceContext.RESTFUL_KEY, false)) {
return null; return null;
} }
// 如果是restful服务
String path = patterns.iterator().next(); String path = patterns.iterator().next();
if (path.contains("$") || isIgnorePattern(path)) { if (path.contains("$") || isIgnorePattern(path)) {
return null; return null;
} }
// 如果是restful服务
String name = path; String name = path;
String prefix = EnvironmentKeys.SOP_RESTFUL_PREFIX.getValue(); String prefix = EnvironmentKeys.SOP_RESTFUL_PREFIX.getValue();
if (StringUtils.isEmpty(prefix)) { if (StringUtils.isEmpty(prefix)) {

@ -17,6 +17,19 @@ import java.lang.reflect.Method;
*/ */
public class ApiMappingHandlerMapping extends RequestMappingHandlerMapping implements PriorityOrdered { 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 @Override
protected RequestCondition<?> getCustomMethodCondition(Method method) { protected RequestCondition<?> getCustomMethodCondition(Method method) {
method.setAccessible(true); method.setAccessible(true);

@ -7,13 +7,8 @@ import org.springframework.util.StringValueResolver;
*/ */
public class ApiMappingStringValueResolver implements StringValueResolver { public class ApiMappingStringValueResolver implements StringValueResolver {
private static final String END_CHAR = "/";
@Override @Override
public String resolveStringValue(String strVal) { public String resolveStringValue(String strVal) {
if (strVal != null && !strVal.endsWith(END_CHAR)) { return strVal;
return strVal + END_CHAR;
}
return null;
} }
} }

@ -1,6 +1,8 @@
package com.gitee.sop.storyweb.controller; package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.util.UploadUtil; import com.gitee.sop.servercommon.util.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data; import lombok.Data;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -20,11 +22,13 @@ import java.util.Collection;
*/ */
@RestController @RestController
@RequestMapping("food") @RequestMapping("food")
@Api(tags = "食物接口")
public class TraditionalWebappController { public class TraditionalWebappController {
// http://localhost:8081/rest/food/getFoodById?id=1 网关入口 // http://localhost:8081/rest/food/getFoodById?id=1 网关入口
// http://localhost:2222/food/getFoodById/?id=12 本地入口 // http://localhost:2222/food/getFoodById/?id=12 本地入口
@ApiOperation(value="获取食物", notes = "根据id获取食物")
@RequestMapping(value = "getFoodById", method = RequestMethod.GET) @RequestMapping(value = "getFoodById", method = RequestMethod.GET)
public Food getFoodById(Integer id) { public Food getFoodById(Integer id) {
Food food = new Food(); Food food = new Food();

@ -8,6 +8,7 @@ import com.gitee.sop.websiteserver.bean.DocModule;
import com.gitee.sop.websiteserver.bean.DocParameter; import com.gitee.sop.websiteserver.bean.DocParameter;
import com.gitee.sop.websiteserver.bean.DocParserContext; import com.gitee.sop.websiteserver.bean.DocParserContext;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -48,6 +49,9 @@ public class SwaggerDocParser implements DocParser {
String method = first.get(); String method = first.get();
JSONObject docInfo = pathInfo.getJSONObject(method); JSONObject docInfo = pathInfo.getJSONObject(method);
DocItem docItem = buildDocItem(docInfo, docRoot); DocItem docItem = buildDocItem(docInfo, docRoot);
if (docItem == null) {
continue;
}
if (docItem.isUploadRequest()) { if (docItem.isUploadRequest()) {
docItem.setHttpMethodList(Sets.newHashSet("post")); docItem.setHttpMethodList(Sets.newHashSet("post"));
} else { } else {
@ -103,8 +107,13 @@ public class SwaggerDocParser implements DocParser {
} }
protected DocItem buildDocItem(JSONObject docInfo, JSONObject docRoot) { protected DocItem buildDocItem(JSONObject docInfo, JSONObject docRoot) {
String apiName = docInfo.getString("sop_name");
// 非开放接口
if (StringUtils.isBlank(apiName)) {
return null;
}
DocItem docItem = new DocItem(); DocItem docItem = new DocItem();
docItem.setName(docInfo.getString("sop_name")); docItem.setName(apiName);
docItem.setVersion(docInfo.getString("sop_version")); docItem.setVersion(docInfo.getString("sop_version"));
docItem.setSummary(docInfo.getString("summary")); docItem.setSummary(docInfo.getString("summary"));
docItem.setDescription(docInfo.getString("description")); docItem.setDescription(docInfo.getString("description"));

Loading…
Cancel
Save