Swagger是一个简单易用的工具,不仅为我们的API调用生成文档,还提供了一个可以援引这些文档的易用的web客户端。今天我们就用spring-boot开发的微服务来集成swagger。
步骤如下:
1、pom文件引入依赖。
2、创建SwaggerConfig类。
3、操作界面进行查看和测试。
- pom文件引入依赖
<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>
- 创建SwaggerConfig类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.Controller"))//指定com.Controller包
.paths(PathSelectors.any())
.build();
}
}
- 操作界面进行查看和测试
我的微服务为jdbc,所以我访问http://localhost:8080/jdbc/swagger-ui.html,
点击List Operations,进入借口列表。
点击进入一个接口,填写参数,点击try。