Browse Source

对swagger文档添加一些初始化配置

lixing 3 years ago
parent
commit
2d6f35e710

+ 9 - 10
apm-common/pom.xml

@@ -100,17 +100,16 @@
             <version>2.9.2</version>
             <scope>compile</scope>
         </dependency>
+<!--        <dependency>-->
+<!--            <groupId>io.swagger</groupId>-->
+<!--            <artifactId>swagger-annotations</artifactId>-->
+<!--            <version>1.5.22</version>-->
+<!--            <scope>compile</scope>-->
+<!--        </dependency>-->
         <dependency>
-            <groupId>io.swagger</groupId>
-            <artifactId>swagger-annotations</artifactId>
-            <version>1.5.22</version>
-            <scope>compile</scope>
-        </dependency>
-        <dependency>
-            <groupId>io.springfox</groupId>
-            <artifactId>springfox-swagger-common</artifactId>
-            <version>2.9.2</version>
-            <scope>compile</scope>
+            <groupId>com.github.xiaoymin</groupId>
+            <artifactId>knife4j-spring-boot-starter</artifactId>
+            <version>2.0.3</version>
         </dependency>
     </dependencies>
 </project>

+ 68 - 1
apm-common/src/main/java/com/persagy/apm/common/config/SwaggerDisplayConfig.java

@@ -3,21 +3,39 @@ package com.persagy.apm.common.config;
 import com.fasterxml.classmate.ResolvedType;
 import com.google.common.base.Optional;
 import com.persagy.apm.common.model.annotation.SwaggerDisplayEnum;
+import com.persagy.common.enums.ResponseCode;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.assertj.core.util.Lists;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Primary;
 import org.springframework.core.annotation.AnnotationUtils;
 import org.springframework.stereotype.Component;
 import org.springframework.util.ReflectionUtils;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 import springfox.documentation.builders.ModelPropertyBuilder;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.builders.ResponseMessageBuilder;
 import springfox.documentation.schema.Annotations;
+import springfox.documentation.schema.ModelRef;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.service.Contact;
+import springfox.documentation.service.ResponseMessage;
 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
 import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
+import springfox.documentation.spring.web.plugins.Docket;
 import springfox.documentation.swagger.schema.ApiModelProperties;
+import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
 import java.lang.reflect.Field;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
@@ -32,8 +50,50 @@ import java.util.stream.Collectors;
 @Component
 @Primary
 @Slf4j
-public class SwaggerDisplayConfig implements ModelPropertyBuilderPlugin {
+@EnableSwagger2
+public class SwaggerDisplayConfig extends WebMvcConfigurationSupport implements ModelPropertyBuilderPlugin {
+    @Value("${swagger.title:接口文档}")
+    private String title;
+    @Value("${swagger.desc:--}")
+    private String desc;
+    @Value("${swagger.version:v1.0.0}")
+    private String version;
+    @Value("${swagger.author:persagy}")
+    private String author;
+    @Value("${swagger.serviceUrl:www.persagy.com}")
+    private String serviceUrl;
+    @Value("${swagger.groupName:api}")
+    private String groupName;
+
+    @Bean
+    public Docket docket() {
+        List<ResponseMessage> responseMessageList = Lists.newArrayList();
+        return new Docket(DocumentationType.SWAGGER_2)
+                .globalResponseMessage(RequestMethod.GET, responseMessageList)
+                .globalResponseMessage(RequestMethod.POST, responseMessageList)
+                .globalResponseMessage(RequestMethod.PUT, responseMessageList)
+                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
+                .apiInfo(apiInfo())
+                .select()
+                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
+                .paths(PathSelectors.any())
+                .build().groupName(groupName);
+    }
 
+    private ApiInfo apiInfo() {
+        //作者信息
+        Contact contact = new Contact(author, "www.persagy.com", "");
+        return new ApiInfo(
+                title,
+                desc,
+                version,
+                serviceUrl,
+                contact,
+                "",
+                "",
+                new ArrayList<>()
+        );
+    }
 
     @Override
     public void apply(ModelPropertyContext context) {
@@ -128,4 +188,11 @@ public class SwaggerDisplayConfig implements ModelPropertyBuilderPlugin {
         return true;
     }
 
+    @Override
+    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
+        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
+        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/META-INF/resources/");
+        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
+    }
 }

apm-screw/doc/energy_report_1.0.0_表结构设计.md → apm-screw/doc/report_factory表结构设计.md


+ 2 - 2
apm-screw/pom.xml

@@ -33,13 +33,13 @@
                 <configuration>
                     <!-- 数据库相关配置 -->
                     <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName>
-                    <jdbcUrl>jdbc:mysql://192.168.100.94:9934/report_factory</jdbcUrl>
+                    <jdbcUrl>jdbc:mysql://192.168.100.94:9934/dmp_alarm</jdbcUrl>
                     <username>root</username>
                     <password>persagy@2020</password>
                     <!-- screw 配置 -->
                     <fileType>MD</fileType>
                     <title>表结构设计</title> <!--标题-->
-                    <fileName>energy_report_1.0.0_表结构设计</fileName> <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
+                    <fileName>dmp_alarm表结构设计</fileName> <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称-->
                     <description>表结构设计</description> <!--描述-->
                     <version>${project.version}</version> <!--版本-->
                     <openOutputDir>false</openOutputDir> <!--打开文件输出目录-->