spring boot项目添加swagger 2.7.0(只需两步操作)
1.pom.xml引入swagger 2.7的jar包
<!-- swagger2 rest api start--> <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> <!-- swagger2 rest api end-->
2.SwaggerConfig.class(放在可以被spring扫码到的地方)
import org.springframework.boot.context.properties.ConfigurationProperties; 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.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author TangZedong * @apiNote swagger2配置文件 * @since 2018/9/4 10:46 */ @Configuration @EnableSwagger2 @ConfigurationProperties public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .forCodeGeneration(true)
.groupName("指定group的名称,groupName不能重复") .select() .apis(RequestHandlerSelectors.basePackage("这里是你需要扫描的包路径")) //过滤生成链接 .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } /** * the api info * * @return api info */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .license("Apache License Version 2.0") .title("blogspot") .description("api docs") .contact(new Contact("tangzedong", "https://www.cnblogs.com/HackerBlog/", "tangzedong.programmer@gmail.com")) .version("1.0") .build(); } }
然后所有的操作就完成了,是不是很简单?就是这么简单
然后启动spring boot服务器,在网页上输入网址:http://localhost:8080/swagger-ui.html 你就可以看见swagger的页面了
我不作恶
但有权拒绝为善
我不赞同
但是我捍卫你不为善的权力