spring-boot集成3:集成swagger
Why swagger?
使用swagger让你从编写接口文档的无聊工作中解脱出来
1.Maven依赖
<!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <!-- swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
2.Swagger配置类
使用自定义Swagger配置类可以自定义一些swagger配置,比如设置要扫描的base package,也可以不用Swagger配置类,使用默认配置。
package com.zhya.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * swagger 配置类 * * @Author zhangyang * @Date 下午 8:57 2018/11/20 0020 **/ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket getDocket() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.zhya.controller")) .paths(PathSelectors.any()) .build(); } }
3.实例