集成Swagger文档
使用Swagger集成文档具有以下几个优势:
●功能丰富:支持多种注解, 自动生成接口文档界面, 支持在界面测试API接口功能。
●及时更新:开发过程中花一点写注释的时间, 就可以及时地更新API文档, 省心省力.
●整合简单:通过添加pom依赖和简单配置, 内嵌于应用中就可同时发布API接口文档界面,不需要部署独立服务.
1.添加依赖
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency>
2.配置类
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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Swagger配置 * @author Louis * @date Jan 11, 2019 */ @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(); } }
3.页面测试
启动应用,在浏览器中访问http://localhost:8080/swagger-ui.html#/