接入 swagger + knife4j 组件,实现 API 接口文档

This commit is contained in:
YunaiV 2021-01-02 13:13:08 +08:00
parent 0e338839d7
commit ca90fcb8fa
7 changed files with 512 additions and 267 deletions

5
http-client.env.json Normal file
View File

@ -0,0 +1,5 @@
{
"local": {
"baseUrl": "http://127.0.0.1:8080"
}
}

595
pom.xml
View File

@ -1,266 +1,329 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.iocoder</groupId>
<artifactId>dashboard</artifactId>
<version>1.0.0</version>
<name>ruoyi</name>
<url>http://www.ruoyi.vip</url>
<description>若依管理系统</description>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<resource.delimiter>@</resource.delimiter>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<!-- 统一依赖管理 -->
<spring.boot.version>2.4.1</spring.boot.version>
<!-- <ruoyi.version>3.3.0</ruoyi.version>-->
<!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>-->
<!-- <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>-->
<!-- <druid.version>1.2.2</druid.version>-->
<!-- <bitwalker.version>1.21</bitwalker.version>-->
<!-- <swagger.version>2.9.2</swagger.version>-->
<!-- <kaptcha.version>2.3.2</kaptcha.version>-->
<!-- <pagehelper.boot.version>1.3.0</pagehelper.boot.version>-->
<!-- <fastjson.version>1.2.74</fastjson.version>-->
<!-- <oshi.version>5.3.6</oshi.version>-->
<!-- <jna.version>5.6.0</jna.version>-->
<!-- <commons.io.version>2.5</commons.io.version>-->
<!-- <commons.fileupload.version>1.3.3</commons.fileupload.version>-->
<!-- <poi.version>4.1.2</poi.version>-->
<!-- <velocity.version>1.7</velocity.version>-->
<!-- <jwt.version>0.9.1</jwt.version>-->
<!-- Web 相关 -->
<!-- DB 相关 -->
<mysql-connector-java.version>5.1.46</mysql-connector-java.version>
<druid.version>1.2.4</druid.version>
<mybatis-plus.version>3.4.1</mybatis-plus.version>
<!-- 工具类相关 -->
<org.projectlombok.version>1.16.14</org.projectlombok.version>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
</properties>
<!-- 依赖声明 -->
<dependencyManagement>
<dependencies>
<!-- &lt;!&ndash;阿里数据库连接池 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid-spring-boot-starter</artifactId>-->
<!-- <version>${druid.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; 解析客户端操作系统、浏览器等 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>eu.bitwalker</groupId>-->
<!-- <artifactId>UserAgentUtils</artifactId>-->
<!-- <version>${bitwalker.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; pagehelper 分页插件 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.pagehelper</groupId>-->
<!-- <artifactId>pagehelper-spring-boot-starter</artifactId>-->
<!-- <version>${pagehelper.boot.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; 获取系统信息 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.oshi</groupId>-->
<!-- <artifactId>oshi-core</artifactId>-->
<!-- <version>${oshi.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>net.java.dev.jna</groupId>-->
<!-- <artifactId>jna</artifactId>-->
<!-- <version>${jna.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>net.java.dev.jna</groupId>-->
<!-- <artifactId>jna-platform</artifactId>-->
<!-- <version>${jna.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; swagger2&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger2</artifactId>-->
<!-- <version>${swagger.version}</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>io.swagger</groupId>-->
<!-- <artifactId>swagger-annotations</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>io.swagger</groupId>-->
<!-- <artifactId>swagger-models</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; swagger2-UI&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger-ui</artifactId>-->
<!-- <version>${swagger.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;io常用工具类 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>commons-io</groupId>-->
<!-- <artifactId>commons-io</artifactId>-->
<!-- <version>${commons.io.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;文件上传工具类 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>commons-fileupload</groupId>-->
<!-- <artifactId>commons-fileupload</artifactId>-->
<!-- <version>${commons.fileupload.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; excel工具 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.apache.poi</groupId>-->
<!-- <artifactId>poi-ooxml</artifactId>-->
<!-- <version>${poi.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;velocity代码生成使用模板 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.apache.velocity</groupId>-->
<!-- <artifactId>velocity</artifactId>-->
<!-- <version>${velocity.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; 阿里JSON解析器 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>fastjson</artifactId>-->
<!-- <version>${fastjson.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;Token生成与解析&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt</artifactId>-->
<!-- <version>${jwt.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;验证码 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.penggle</groupId>-->
<!-- <artifactId>kaptcha</artifactId>-->
<!-- <version>${kaptcha.version}</version>-->
<!-- </dependency>-->
<!-- 统一依赖管理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- <modules>-->
<!-- <module>ruoyi-admin</module>-->
<!-- <module>ruoyi-framework</module>-->
<!-- <module>ruoyi-system</module>-->
<!-- <module>ruoyi-quartz</module>-->
<!-- <module>ruoyi-generator</module>-->
<!-- <module>ruoyi-common</module>-->
<!-- </modules>-->
<packaging>jar</packaging>
<dependencies>
<!-- Web 相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DB 相关 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- 工具类相关 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- 提供给 mapstruct 使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source> <!-- or higher, depending on your project -->
<target>${java.version}</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.iocoder</groupId>
<artifactId>dashboard</artifactId>
<version>1.0.0</version>
<name>ruoyi</name>
<url>http://www.ruoyi.vip</url>
<description>若依管理系统</description>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<resource.delimiter>@</resource.delimiter>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<!-- 统一依赖管理 -->
<spring.boot.version>2.4.1</spring.boot.version>
<!-- <ruoyi.version>3.3.0</ruoyi.version>-->
<!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>-->
<!-- <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>-->
<!-- <druid.version>1.2.2</druid.version>-->
<!-- <bitwalker.version>1.21</bitwalker.version>-->
<!-- <swagger.version>2.9.2</swagger.version>-->
<!-- <kaptcha.version>2.3.2</kaptcha.version>-->
<!-- <pagehelper.boot.version>1.3.0</pagehelper.boot.version>-->
<!-- <fastjson.version>1.2.74</fastjson.version>-->
<!-- <oshi.version>5.3.6</oshi.version>-->
<!-- <jna.version>5.6.0</jna.version>-->
<!-- <commons.io.version>2.5</commons.io.version>-->
<!-- <commons.fileupload.version>1.3.3</commons.fileupload.version>-->
<!-- <poi.version>4.1.2</poi.version>-->
<!-- <velocity.version>1.7</velocity.version>-->
<!-- <jwt.version>0.9.1</jwt.version>-->
<!-- Web 相关 -->
<knife4j.version>3.0.2</knife4j.version>
<!-- DB 相关 -->
<mysql-connector-java.version>5.1.46</mysql-connector-java.version>
<druid.version>1.2.4</druid.version>
<mybatis-plus.version>3.4.1</mybatis-plus.version>
<redisson.version>3.13.6</redisson.version>
<!-- 工具类相关 -->
<org.projectlombok.version>1.16.14</org.projectlombok.version>
<org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
<jjwt.version>0.9.1</jjwt.version>
<fastjson.version>1.2.75</fastjson.version>
<hutool.version>5.5.6</hutool.version>
</properties>
<!-- 依赖声明 -->
<dependencyManagement>
<dependencies>
<!-- &lt;!&ndash;阿里数据库连接池 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>druid-spring-boot-starter</artifactId>-->
<!-- <version>${druid.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; 解析客户端操作系统、浏览器等 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>eu.bitwalker</groupId>-->
<!-- <artifactId>UserAgentUtils</artifactId>-->
<!-- <version>${bitwalker.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; pagehelper 分页插件 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.pagehelper</groupId>-->
<!-- <artifactId>pagehelper-spring-boot-starter</artifactId>-->
<!-- <version>${pagehelper.boot.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; 获取系统信息 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.oshi</groupId>-->
<!-- <artifactId>oshi-core</artifactId>-->
<!-- <version>${oshi.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>net.java.dev.jna</groupId>-->
<!-- <artifactId>jna</artifactId>-->
<!-- <version>${jna.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>net.java.dev.jna</groupId>-->
<!-- <artifactId>jna-platform</artifactId>-->
<!-- <version>${jna.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; swagger2&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger2</artifactId>-->
<!-- <version>${swagger.version}</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>io.swagger</groupId>-->
<!-- <artifactId>swagger-annotations</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>io.swagger</groupId>-->
<!-- <artifactId>swagger-models</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; swagger2-UI&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger-ui</artifactId>-->
<!-- <version>${swagger.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;io常用工具类 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>commons-io</groupId>-->
<!-- <artifactId>commons-io</artifactId>-->
<!-- <version>${commons.io.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;文件上传工具类 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>commons-fileupload</groupId>-->
<!-- <artifactId>commons-fileupload</artifactId>-->
<!-- <version>${commons.fileupload.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; excel工具 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.apache.poi</groupId>-->
<!-- <artifactId>poi-ooxml</artifactId>-->
<!-- <version>${poi.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;velocity代码生成使用模板 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.apache.velocity</groupId>-->
<!-- <artifactId>velocity</artifactId>-->
<!-- <version>${velocity.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; 阿里JSON解析器 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>fastjson</artifactId>-->
<!-- <version>${fastjson.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;Token生成与解析&ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt</artifactId>-->
<!-- <version>${jwt.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;验证码 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.penggle</groupId>-->
<!-- <artifactId>kaptcha</artifactId>-->
<!-- <version>${kaptcha.version}</version>-->
<!-- </dependency>-->
<!-- 统一依赖管理 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- <modules>-->
<!-- <module>ruoyi-admin</module>-->
<!-- <module>ruoyi-framework</module>-->
<!-- <module>ruoyi-system</module>-->
<!-- <module>ruoyi-quartz</module>-->
<!-- <module>ruoyi-generator</module>-->
<!-- <module>ruoyi-common</module>-->
<!-- </modules>-->
<packaging>jar</packaging>
<dependencies>
<!-- Spring 核心 -->
<dependency>
<!-- 用于生成自定义的 Spring @ConfigurationProperties 配置类的说明文件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!-- DB 相关 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson.version}</version>
</dependency>
<!-- 工具类相关 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
<dependency>
<!-- 解析客户端操作系统、浏览器等 TODO 芋艿:后面看看 hutool 有没替代 -->
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- 提供给 mapstruct 使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source> <!-- or higher, depending on your project -->
<target>${java.version}</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,105 @@
package cn.iocoder.dashboard.framework.swagger.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.service.ApiKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static springfox.documentation.builders.RequestHandlerSelectors.basePackage;
/**
* Swagger2 自动配置类
*
* @author 芋道源码
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
@ConditionalOnClass({Docket.class, ApiInfoBuilder.class})
@ConditionalOnProperty(prefix = "yudao.swagger", value = "enable", matchIfMissing = true)
// 允许使用 swagger.enable=false 禁用 Swagger
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties() {
return new SwaggerProperties();
}
@Bean
public Docket createRestApi() {
SwaggerProperties properties = swaggerProperties();
// 创建 Docket 对象
return new Docket(DocumentationType.SWAGGER_2)
// 用来创建该 API 的基本信息展示在文档的页面中自定义展示的信息
.apiInfo(apiInfo(properties))
// 设置扫描指定 package 包下的
.select()
.apis(basePackage(properties.getBasePackage()))
.paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts())
// .pathMapping() TODO 芋艿稍后解决统一 api 前缀
;
}
/**
* API 摘要信息
*/
private static ApiInfo apiInfo(SwaggerProperties properties) {
return new ApiInfoBuilder()
.title(properties.getTitle())
.description(properties.getDescription())
.contact(new Contact(properties.getAuthor(), null, null))
.version(properties.getVersion())
.build();
}
/**
* 安全模式这里配置通过请求头 Authorization 传递 token 参数
*/
private static List<SecurityScheme> securitySchemes() {
return Collections.singletonList(new ApiKey(HttpHeaders.AUTHORIZATION, "Authorization", "header"));
}
/**
* 安全上下文
*
* @see #securitySchemes()
* @see #authorizationScopes()
*/
private static List<SecurityContext> securityContexts() {
return Collections.singletonList(SecurityContext.builder()
.securityReferences(securityReferences())
.forPaths(PathSelectors.regex("^(?!auth).*$"))
.build());
}
private static List<SecurityReference> securityReferences() {
return Collections.singletonList(new SecurityReference(HttpHeaders.AUTHORIZATION, authorizationScopes()));
}
private static AuthorizationScope[] authorizationScopes() {
return new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")};
}
}

View File

@ -0,0 +1,43 @@
package cn.iocoder.dashboard.framework.swagger.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.validation.constraints.NotEmpty;
/**
* Swagger 配置属性
*
* @author 芋道源码
*/
@ConfigurationProperties("yudao.swagger")
@Data
public class SwaggerProperties {
/**
* 标题
*/
@NotEmpty(message = "标题不能为空")
private String title;
/**
* 描述
*/
@NotEmpty(message = "描述不能为空")
private String description;
/**
* 作者
*/
@NotEmpty(message = "作者不能为空")
private String author;
/**
* 版本
*/
@NotEmpty(message = "版本不能为空")
private String version;
/**
* 扫描的包
*/
@NotEmpty(message = "扫描的 package 不能为空")
private String basePackage;
}

View File

@ -0,0 +1,6 @@
/**
* 基于 Swagger + Knife4j 实现 API 接口文档
*
* @author 芋道源码
*/
package cn.iocoder.dashboard.framework.swagger;

View File

@ -0,0 +1 @@
<http://www.iocoder.cn/Spring-Boot/Swagger/?dashboard>

View File

@ -1,7 +1,29 @@
spring:
# 数据源配置项
# 数据源配置项 TODO 多数据源TODO 监控配置
datasource:
url: jdbc:mysql://127.0.1:3306/ruoyi-vue-pro?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
# redis 配置 TODO 需要进行下,对接到 redisson
redis:
# 地址
host: localhost
# 端口默认为6379
port: 6379
# 数据库索引
database: 0
# 连接超时时间
timeout: 10s
# 芋道配置项,设置当前项目所有自定义的配置
yudao:
security:
token-header: Authorization
token-secret: abcdefghijklmnopqrstuvwxyz
token-expires: 30m
swagger:
title: 管理后台
description: 提供管理员管理的所有功能
version: 1.0.0
base-package: cn.iocoder.dashboard.modules