本文接著前面的繼續(xù),介紹如何快速接入
Swagger
- Spring Boot七分鐘快速實踐
- Spring Boot & MyBatis
- Spring Boot & Redis
- Spring Boot & Swagger
- Spring Boot & 單元測試
- Spring Boot & Actuator
- Spring Boot Admin
兩步接入
- pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 代碼配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
展示
數(shù)據(jù)接口
http://localhost:8080/swagger-ui.html

Swagger文檔展示
已經(jīng)實現(xiàn)了最簡單的接入了
其他配置
- 限制開發(fā)環(huán)境展示
使用Spring條件注入特性@Profile("dev")
@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
測試環(huán)境啟動命令如下
java -jar boot-web-2.1.2.RELEASE.jar -Dspring.profiles.active=dev
- 限制部分接口
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.tenmao.mvc.controller"))
.paths(PathSelectors.ant("/api/*"))
.build();
}
- 自定義文檔信息
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.tenmao.mvc.controller"))
.paths(PathSelectors.ant("/api/*"))
.build()
// 這里有一個調(diào)用
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"十毛API",
"十毛API文檔",
"1.0",
"Terms of service",
new Contact("tenmao", "http://m.itdecent.cn/u/518bde83a9bc", "tenmao@example.com"),
"License of API", "API license URL", Collections.emptyList());
}