本文的内容为:在SpringBoot项目中集成Swagger2,并且更换第三方UI
目录
在项目的POM文件中引入需要的资源GAV
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<!-- swagger2官方UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<!-- 第三方UI -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
增加配置类
package com.imooc.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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @description: Swagger2配置类
* @author: WhiteCrowZHZ
* @date: 2022/2/16 7:52
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* 配置swagger2核心配置
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
.apiInfo(apiInfo()) // 定义api文档汇总信息
.select().apis(RequestHandlerSelectors.basePackage("com.imooc.controller")) // 指定需要提供文档的Controller类所在的包
.paths(PathSelectors.any()) // 需要生成文档的接口路径
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("这是接口文档页的标题")
.contact(new Contact(
"name",
"http://example.com",
"example@qq.com"))
.description("这是一段关于接口文档的描述信息")
.version("1.0.1")
.termsOfServiceUrl("http://example.com")
.build();
}
}
启动项目,访问文档
// 官方ui访问地址
http://localhost:8088/swagger-ui.html
// 第三方ui访问地址
http://localhost:8088/doc.html
访问效果
官方UI:
第三方UI: