概要
简单分享关于SpringBoot(maven)集成swagger api接口文档的方法,此处使用的界面ui为BootstrapUi,其界面如下
主要过程
1.添加依赖
在pom文件中添加以下依赖
<!-- 由于是springfox-swagger的增强UI包,所以基础功能依然依赖Swagger,springfox-swagger的jar包必须引入 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 引入SwaggerBootstrapUi的jar包 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
注:可能maven找不到swagger-bootstrap-ui的依赖,则加入下方的仓库配置
仓库中setting.xml内<mirrorOf>*</mirrorOf>
设为通配符,则会过滤代理所有的仓库,则会使添加的仓库无效。
<repository>
<id>com.github.xiaoymin</id>
<name>bootstrapUi</name>
<url>https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
2.编写Swagger2Config配置文件
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bycdao.cloud"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger-bootstrap-ui RESTful APIs")
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("http://localhost:8999/")
.contact("developer@mail.com")
.version("1.0")
.build();
}
}
3.运行项目并访问端口
默认访问地址是:http://host:port/doc.html
例如 http://localhost:8080/doc.html