370774330@qq.com 5 лет назад
Родитель
Сommit
1896967c6f
2 измененных файлов с 90 добавлено и 0 удалено
  1. 14 0
      web/pom.xml
  2. 76 0
      web/src/main/java/seecoder/devcloud/web/config/SwaggerConfig.java

+ 14 - 0
web/pom.xml

@@ -23,6 +23,20 @@
 
     <dependencies>
 
+        <!-- Swagger -->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger-ui</artifactId>
+            <version>2.2.2</version>
+        </dependency>
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger2</artifactId>
+            <version>2.2.2</version>
+        </dependency>
+        <!-- END Swagger -->
+
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-thymeleaf</artifactId>

+ 76 - 0
web/src/main/java/seecoder/devcloud/web/config/SwaggerConfig.java

@@ -0,0 +1,76 @@
+package seecoder.devcloud.web.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.http.ResponseEntity;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+
+import java.util.function.Predicate;
+
+import static springfox.documentation.builders.PathSelectors.regex;
+
+/**
+ * @author PuHong Weng
+ * @date 2021/2/3
+ * @description:
+ */
+public class SwaggerConfig {
+
+    @Value("${server.servlet-path}")
+    private String pathMapping;
+
+    private ApiInfo initApiInfo() {
+        ApiInfo apiInfo = new ApiInfo("seecoder devcloud项目 Platform API",//大标题
+                initContextInfo(),//简单的描述
+                "0.0.1",//版本
+                "服务条款",
+                "后台开发团队",//作者
+                "The xxxxxxx License, Version",//todo 链接显示文字
+                "http://www.xxxx.com"//todo 网站链接
+        );
+
+        return apiInfo;
+    }
+
+    private String initContextInfo() {
+        StringBuffer sb = new StringBuffer();
+        sb.append("Devclound API");
+        return sb.toString();
+    }
+
+
+    @Bean
+    public Docket restfulApi() {
+        System.out.println("http://localhost:8080" + pathMapping + "/swagger-ui.html");
+
+        return new Docket(DocumentationType.SWAGGER_2)
+                .groupName("RestfulApi")
+//                .genericModelSubstitutes(DeferredResult.class)
+                .genericModelSubstitutes(ResponseEntity.class)
+                .useDefaultResponseMessages(true)
+                .forCodeGeneration(false)
+                .pathMapping(pathMapping) // base,最终调用接口后会和paths拼接在一起
+                .select()
+                //.paths(doFilteringRules())
+                .paths(PathSelectors.any())
+                .build()
+                .apiInfo(initApiInfo());
+    }
+
+    /**
+     * 有些重要api可能不想在swagger网页里展示,在这里可以过滤
+     * 设置过滤规则
+     * 这里的过滤规则支持正则匹配
+     * @return
+     */
+//    private Predicate<String> doFilteringRules() {
+//        return or(
+//                regex("/hello.*"),
+//                regex("/vehicles.*")
+//        );
+//    }
+
+}