SpringBoot集成Swagger文档
Swagger2很好的解决了多人协作中共享和及时更新API开发文档的问题
Swagger优势有很多,诸如支持多种注解,自动生成接口文档界面并且支持在界面测试API接口功能;同时只要你在开发时多花点时间写注解,Swagger就能够及时更新API文档;最后Swagger是可以通过配置pom文件将内嵌于你的项目中,不用再独立部署。
首先需要配置pom文件
<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>
编写Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder().build();
}
}
打开localhost:8080//swagger-uui.html#/进入到接口文档界面,就可以选择接口进行测试了
点击try it out ----》execute就可以看到服务运行之后所返回的信息