Spring Boot & Swagger
2019-01-31 本文已影响61人
十毛tenmao
本文接着前面的继续,介绍如何快速接入
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();
}
}
展示
Swagger文档展示已经实现了最简单的接入了
其他配置
- 限制开发环境展示
使用Spring条件注入特性@Profile("dev")
@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
测试环境启动命令如下
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()
// 这里有一个调用
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"十毛API",
"十毛API文档",
"1.0",
"Terms of service",
new Contact("tenmao", "https://www.jianshu.com/u/518bde83a9bc", "tenmao@example.com"),
"License of API", "API license URL", Collections.emptyList());
}