diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java index ca179daf..10efd4cd 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java @@ -5,6 +5,8 @@ import com.gitee.sop.servercommon.annotation.ApiMapping; import com.gitee.sop.servercommon.bean.ServiceConfig; import com.gitee.sop.servercommon.mapping.RouteUtil; import com.google.common.base.Optional; +import io.swagger.annotations.Api; +import org.springframework.core.annotation.Order; import springfox.documentation.service.Operation; import springfox.documentation.service.StringVendorExtension; import springfox.documentation.service.VendorExtension; @@ -20,6 +22,7 @@ public class DocumentationPluginsManagerExt extends DocumentationPluginsManager public static final String SOP_NAME = "sop_name"; public static final String SOP_VERSION = "sop_version"; + public static final String MODULE_ORDER = "module_order"; @Override public Operation operation(OperationContext operationContext) { @@ -48,6 +51,17 @@ public class DocumentationPluginsManagerExt extends DocumentationPluginsManager vendorExtensions.add(new StringVendorExtension(SOP_VERSION, version)); } } + Optional apiOptional = operationContext.findControllerAnnotation(Api.class); + int order = 0; + if (apiOptional.isPresent()) { + order = apiOptional.get().position(); + } else { + Optional orderOptional = operationContext.findControllerAnnotation(Order.class); + if (orderOptional.isPresent()) { + order = orderOptional.get().value(); + } + } + vendorExtensions.add(new StringVendorExtension(MODULE_ORDER, String.valueOf(order))); } private String buildVersion(String version) { diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/DownloadController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/DownloadController.java index 044f0c2d..2b41bb9d 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/DownloadController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/DownloadController.java @@ -18,7 +18,7 @@ import java.io.IOException; * * @author tanghc */ -@Api(tags = "文件下载") +@Api(tags = "文件下载", position = 3) @Controller public class DownloadController { diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/FileUploadDemoController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/FileUploadDemoController.java index 65bf7e8f..ff57a354 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/FileUploadDemoController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/FileUploadDemoController.java @@ -23,7 +23,7 @@ import java.util.Collection; * @author tanghc */ @RestController -@Api(tags = "文件上传") +@Api(tags = "文件上传", position = 2) public class FileUploadDemoController { /** diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java index cc62daaf..2f68b9ef 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java @@ -22,7 +22,7 @@ public class DocItem { private Collection produces; - + private int moduleOrder; List requestParameters; List responseParameters; diff --git a/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocModule.java b/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocModule.java index 4a237b9f..d51d1c8c 100644 --- a/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocModule.java +++ b/sop-website/src/main/java/com/gitee/sop/websiteserver/bean/DocModule.java @@ -11,4 +11,5 @@ import java.util.List; public class DocModule { private String module; private List docItems; + private int order; } 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 26a1d25c..ae83b3ab 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,11 +8,14 @@ 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.lang3.math.NumberUtils; import org.springframework.beans.BeanUtils; +import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -55,20 +58,30 @@ public class SwaggerDocParser implements DocParser { .entrySet() .stream() .map(entry -> { + List docItemList = entry.getValue(); DocModule docModule = new DocModule(); docModule.setModule(entry.getKey()); - docModule.setDocItems(entry.getValue()); + docModule.setDocItems(docItemList); + docModule.setOrder(getMuduleOrder(docItemList)); return docModule; - }) + }).sorted(Comparator.comparing(DocModule::getOrder)) .collect(Collectors.toList()); - DocInfo docInfo = new DocInfo(); docInfo.setTitle(title); docInfo.setDocModuleList(docModuleList); return docInfo; } + private int getMuduleOrder(List items) { + if (CollectionUtils.isEmpty(items)) { + return Integer.MAX_VALUE; + } + List docItemList = new ArrayList<>(items); + docItemList.sort(Comparator.comparing(DocItem::getModuleOrder)); + return docItemList.get(0).getModuleOrder(); + } + protected Collection getHttpMethods(JSONObject pathInfo) { // key: get,post,head... List retList; @@ -93,6 +106,7 @@ public class SwaggerDocParser implements DocParser { docItem.setDescription(docInfo.getString("description")); docItem.setMultiple(docInfo.getString("multiple") != null); docItem.setProduces(docInfo.getJSONArray("produces").toJavaList(String.class)); + docItem.setModuleOrder(NumberUtils.toInt(docInfo.getString("module_order"), 0)); String moduleName = this.buildModuleName(docInfo, docRoot); docItem.setModule(moduleName); List docParameterList = this.buildRequestParameterList(docInfo, docRoot); diff --git a/sop-website/src/main/resources/public/pages/doc/doc.js b/sop-website/src/main/resources/public/pages/doc/doc.js index b6ec10f7..f7b0db2b 100644 --- a/sop-website/src/main/resources/public/pages/doc/doc.js +++ b/sop-website/src/main/resources/public/pages/doc/doc.js @@ -48,7 +48,7 @@ layui.config({ html.push('
  • ' + module.module + '

  • '); for (var i = 0; i < docItems.length; i++) { var docItem = docItems[i]; - var first = j == 0 && j == 0; + var first = j === 0 && i === 0; if (first) { firstItem = docItem; }