springboot集成 swagger3
- 加入坐标
<!-- swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
- 创建配置类 SwaggerConfig
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
*
* @author :
* @version : 1.0
* @project : springboot-swagger3-demo
* @package : com.cunyu.springbootswagger3demo.config
* @className : SwaggerConfig
* @createTime :
* @email :
* @微信 :
* @公众号 :
* @网站 :
* @description :
*/
@Configuration
@EnableOpenApi
public class SwaggerConfig {
/**
* 用于读取配置文件 application.properties 中 swagger 属性是否开启
*/
@Value("${swagger.enabled}")
Boolean swaggerEnabled;
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
// 是否开启swagger
.enable(swaggerEnabled)
.select()
// 过滤条件,扫描指定路径下的文件
.apis(RequestHandlerSelectors.basePackage("com.cunyu.springbootswagger3demo.controller"))
// 指定路径处理,PathSelectors.any()代表不过滤任何路径
//.paths(PathSelectors.any())
.build();
}
/**
* swagger3 访问地址
* http://localhost:9090/swagger-ui/index.html
*
*/
private ApiInfo apiInfo() {
/*作者信息*/
Contact contact = new Contact("村雨遥", "https://cunyu1943.github.io", "747731461@qq.com");
return new ApiInfo(
"Spring Boot 集成 Swagger3 测试",
"Spring Boot 集成 Swagger3 测试接口文档",
"v1.0",
"https://cunyu1943.github.io",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
- 访问地址
http://localhost:9090/swagger-ui/index.html
遇到的bug
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
解决方案
spring-boot-starter-parent版本使用2.5.7
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.7</version> <!-- 启动版本使用2.5.7 -->
<relativePath/> <!-- lookup parent from repository -->
</parent>