SpringBoot 2.3.12集成Swagger3.0
增加 maven 依赖
<!-- swagger配置依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-metadata</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-metadata</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
如果不增加 spring-plugin-core、spring-plugin-metadata 的额外配置, springfox 依赖 的默认版本为 1.2.0 ,启动时会报方法找不到错误,因此需要额外指定 2.0.0 版本。
增加配置类
package com.everelegance.boccfc.channelcenter.be.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;
@Configuration
public class Swagger3Config {
Boolean swaggerEnabled = true;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
// 是否开启
.enable(swaggerEnabled)
.select()
// 指定接口的位置
.apis(RequestHandlerSelectors.basePackage("com.xxx.xxxx"))
.paths(PathSelectors.any())
.build();
}
/**
* 配置网站的基本信息
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 网站标题
.title("xxxx服务接口文档")
// 联系人信息
.contact( new Contact("","", "xxx@qq.com"))
// 版本号
.version("1.0")
// 描述
.description("xxx")
.build();
}
}
启动类增加注解
springboot 启动类 增加以下注解:
@EnableOpenApi