一、springBoot集成swagger
参看文档:https://springfox.github.io/springfox/docs/current/#springfox-swagger-ui
1. 集成方式
- Maven集成
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
- Gradle集成
repositories {
jcenter()
}
dependencies {
compile "io.springfox:springfox-swagger2:2.7.0"
compile "io.springfox:springfox-swagger-ui:2.7.0"
}
2. 注入swagger配置bean
/**
* @ClassName SwaggerConfig
* @Description TODO
* @Author eastern
* @Date 2019/8/12 下午4:41
* @Version 1.0
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket controllerApi(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("controller")
.apiInfo(apiInfo())
.select()
// 只显示xx Controller下的api 包路径
.apis(RequestHandlerSelectors.basePackage(ProductController.class.getPackage().getName()))
// 只显示以products开头的api
// .paths(PathSelectors.ant("/products/*"))
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("HTTP API")
.description("Swagger测试接口")
.termsOfServiceUrl("http://springfox.io")
.license("Apache License Version 2.0")
.contact(new Contact("zhangsan", "", "xxxxxxx@qq.com"))
.version("2.0")
.build();
}
}