springboot 整合 swagger 在线文档生成工具
导入依赖
knife4j
<!-- 在线接口文档生成工具 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
配置数据源
application.properties
#数据源配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 使用阿里的Druid连接池
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 填写你数据库的url、登录名、密码和数据库名
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
# 连接池的配置信息
# 初始化大小,最小,最大
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.maxActive=20
# 配置获取连接等待超时的时间-1min
spring.datasource.druid.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
# 解决swagger报空指针
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
编写swagger配置类
SwaggerConfig.class
package com.sheep.emo.config;
/**
* @author : sheep669
* @description : swagger配置类
* @created at 2022/7/9 23:08
*/
import io.swagger.annotations.Api;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.omg.CORBA.Request;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 创建接口文档信息
*/
public ApiInfo createApiInfo() {
return new ApiInfoBuilder()
.title("emo在线团购系统后台接口文档")
.description("学习永无止境")
.version("V1.0.0")
.termsOfServiceUrl("https://blog.nowcoder.net/visya")
.contact(new Contact("sheep669",
"https://blog.nowcoder.net/visya",
"sheep669@126.com"))
.build();
}
@Bean
public Docket createApi() {
return new Docket(DocumentationType.SWAGGER_2).
apiInfo(createApiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.build();
}
}
启动项目,访问文档地址
http://localhost:8080/doc.html
Over 感谢查阅