特点
1.自动生成API的工具
2.开源
结果展示
使用
pom依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swagger 配置类
package com.bcm.task.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;
/**
* Swagger2 配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* Docket实例
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bcm.task.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* Swagger apiInfo
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API文档")
.contact(new Contact("信息部", "http://172.38.9.32:9999", "1759043640@qq.com"))
.version("1.0")
.description("Api 描述")
.build();
}
}
启动访问查看效果
http://localhost:8080/swagger-ui.html