Bladeren bron

标准 Swagger 生成

赵平西 5 jaren geleden
bovenliggende
commit
cc8429ce5f
1 gewijzigde bestanden met toevoegingen van 26 en 12 verwijderingen
  1. 26 12
      swagger-support/SwaggerExtentionSupport.java

+ 26 - 12
swagger-support/SwaggerExtentionSupport.java

@@ -100,6 +100,8 @@ public class SwaggerExtentionSupport implements ApplicationListener<ContextRefre
             Multimap<String, ApiListing> apiListings = documentation.getApiListings();
             //Swagger swagger = mapper.mapDocumentation(documentation);
             String[] beanDefinitionNames = context.getBeanDefinitionNames();
+
+            //String[] beanDefinitionNames = new String[]{"test.do", "param.do", "rule.do"};
             for (String name : beanDefinitionNames) {
                 if(name.endsWith(".do")) {
                     Class<?> aClass = context.getBean(name).getClass();
@@ -134,10 +136,11 @@ public class SwaggerExtentionSupport implements ApplicationListener<ContextRefre
                             String methodName) {
         // 获取去除了 .do 的 名称
         String optGroup = getName(beanName);
-        String optId = optGroup + "_" + methodName;
+        String apiId = optGroup + "_" + methodName;
+        String optId = methodName;
 
         // 生成唯一ID
-        Collection<ApiListing> apis = apiListings.get(optId);
+        Collection<ApiListing> apis = apiListings.get(apiId);
         if(apis == null) {
             // 后面只是用 apis 的 size 获取长度
             apis = new HashSet<>();
@@ -147,12 +150,18 @@ public class SwaggerExtentionSupport implements ApplicationListener<ContextRefre
 
         ArrayList<Operation> operations = new ArrayList<>();
 
-        ResponseMessageBuilder v1 = new ResponseMessageBuilder();
-        v1.code(200).message("OK");
+        ResponseMessage v200 = new ResponseMessageBuilder().code(200).message("OK").build();
+        ResponseMessage v401 = new ResponseMessageBuilder().code(401).message("Unauthorized").build();
+        ResponseMessage v403 = new ResponseMessageBuilder().code(403).message("Forbidden").build();
+        ResponseMessage v404 = new ResponseMessageBuilder().code(404).message("Not Found").build();
 
         // tag
         HashSet<Tag> tags = new HashSet<>();
-        tags.add(new Tag(optGroup, beanName+"." + methodName));
+
+        // description 是生成 API JS 的文件名
+        // optGroup 是生成的函数名
+        Tag tag = new Tag(optGroup, optGroup + "Controller");
+        tags.add(tag);
 
         // 注意 position,必须是不重复的值
         Operation operaGet = new Operation(
@@ -162,39 +171,42 @@ public class SwaggerExtentionSupport implements ApplicationListener<ContextRefre
                 new ModelRef("ResponseCode"),
                 optId+"UsingGET",
                 0,
-                Sets.newHashSet(optGroup),
+                Sets.newHashSet(tag.getName()),
                 Sets.newHashSet(MediaType.ANY_TYPE.toString()),
                 Sets.newHashSet(MediaType.create("application", "json").toString()),
                 new HashSet<>(),
                 new ArrayList<>(),
                 new ArrayList<>(),
-                Sets.newHashSet(v1.build()),
+                Sets.newHashSet(v200, v401, v403, v404),
                 "",
                 false,
                 new ArrayList<>()
         );
 
+        // Operation 只需要 tag name,他决定了该 api 在 Swagger 上挂载的tag
+        /*
         Operation operaPost = new Operation(
                 HttpMethod.POST,
                 "do exec " + optGroup + "." + methodName,
                 "",
                 new ModelRef("ResponseCode"),
                 optId+"UsingPOST",
-                1,
-                Sets.newHashSet(optGroup),
+                0,
+                Sets.newHashSet(tag.getName()),
                 Sets.newHashSet(MediaType.ANY_TYPE.toString()),
                 Sets.newHashSet(MediaType.create("application", "json").toString()),
                 new HashSet<>(),
                 new ArrayList<>(),
                 new ArrayList<>(),
-                Sets.newHashSet(v1.build()),
+                Sets.newHashSet(v200, v401, v403, v404),
                 "",
                 false,
                 new ArrayList<>()
         );
+         */
 
         operations.add(operaGet);
-        operations.add(operaPost);
+        //operations.add(operaPost);
 
         String url = "/" + beanName + "?invoke=" + methodName;
         apis1.add(new ApiDescription(groupName,
@@ -212,7 +224,9 @@ public class SwaggerExtentionSupport implements ApplicationListener<ContextRefre
                 new HashMap<>(), beanName+"." + methodName, apis.size(), tags);
 
         // 放到api列表中
-        apiListings.put(optId, apiListing);
+        apiListings.put(apiId, apiListing);
+
+        // 返回 tag,tag 会显示到 Swagger Content
         return tags;
     }