SpringBoot 显示Swagger Api 文档
1. 配置pom文件,在pom文件中引入Swagger的相关依赖:
<!-- swagger --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>com.spring4all</groupId> <artifactId>swagger-spring-boot-starter</artifactId> <version>1.9.0.RELEASE</version> </dependency>
2.在application.yml 文件中配置相关 swagger 参数
2-1. 配置说明--默认配置:
swagger.enabled=是否启用swagger,默认:true
swagger.title=标题
swagger.description=描述
swagger.version=版本
swagger.license=许可证
swagger.licenseUrl=许可证URL
swagger.termsOfServiceUrl=服务条款URL
swagger.contact.name=维护人
swagger.contact.url=维护人URL
swagger.contact.email=维护人email
swagger.base-package=swagger扫描的基础包,默认:全扫描
swagger.base-path=需要处理的基础URL规则,默认:/**
swagger.exclude-path=需要排除的URL规则,默认:空
swagger.host=文档的host信息,默认:空
2-2. Authorization 鉴权配置
# 鉴权策略ID,对应 SecurityReferences ID swagger.authorization.name=Authorization # 鉴权策略,可选 ApiKey | BasicAuth | None,默认ApiKey swagger.authorization.type=ApiKey # 鉴权传递的Header参数 swagger.authorization.key-name=token # 鉴权传递的Header参数名称 swagger.authorization.name=token # 需要开启鉴权URL的正则, 默认^.*$匹配所有URL swagger.authorization.auth-regex=^.*$
说明:其他具体的 参数配置 :https://github.com/SpringForAll/spring-boot-starter-swagger
3.在项目启动入口添加注解 @EnableSwagger2Doc
import com.spring4all.swagger.EnableSwagger2Doc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableSwagger2Doc @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
4.启动项目访问地址 :http://localhost:8005/doc.html,内容如下: