简化Swagger使用的自制Starter:spring-boot-starter-swagger,欢迎使用和吐槽
項目簡介
該項目主要利用Spring Boot的自動化配置特性來實現快速的將swagger2引入spring boot應用來生成API文檔,簡化原生使用swagger2的整合代碼。
GitHub:https://github.com/dyc87112/spring-boot-starter-swagger
碼云:http://git.oschina.net/didispace/spring-boot-starter-swagger
博客:http://blog.didispace.com
小工具一枚,歡迎使用和Star支持,如使用過程中碰到問題,可以提出Issue,我會盡力完善該Starter
版本基礎
- Spring Boot:1.5.x
- Swagger:2.7.x
如何使用
在該項目的幫助下,我們的Spring Boot可以輕松的引入swagger2,主需要做下面兩個步驟:
- 在pom.xml中引入依賴:
| <dependency> <groupId>com.didispace</groupId> <artifactId>spring-boot-starter-swagger</artifactId> <version>1.1.0.RELEASE</version> </dependency> |
- 在應用主類中增加@EnableSwagger2Doc注解
| 2Doc public class Bootstrap { public static void main(String[] args) { SpringApplication.run(Bootstrap.class, args); } } |
默認情況下就能產生所有當前Spring MVC加載的請求映射文檔。
參數配置
更細致的配置內容參考如下:
配置示例
| swagger.title=spring-boot-starter-swagger swagger.description=Starter for swagger 2.x swagger.version=1.1.0.RELEASE swagger.license=Apache License, Version 2.0 swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger swagger.contact.name=程序猿DD swagger.contact.url=http://blog.didispace.com swagger.contact.email=dyc87112@qq.com swagger.base-package=com.didispace swagger.base-path=/** swagger.exclude-path=/error, /ops/** |
配置說明
- 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規則,默認:空
Path規則說明
swagger.base-path和swagger.exclude-path使用ANT規則配置。
我們可以使用swagger.base-path來指定所有需要生成文檔的請求路徑基礎規則,然后再利用swagger.exclude-path來剔除部分我們不需要的。
比如,通常我們可以這樣設置:
| management.context-path=/ops swagger.base-path=/** swagger.exclude-path=/ops/**, /error |
上面的設置將解析所有除了/ops/開始以及spring boot自帶/error請求路徑。
其中,exclude-path可以配合management.context-path=/ops設置的spring boot actuator的context-path來排除所有監控端點。
總結
以上是生活随笔為你收集整理的简化Swagger使用的自制Starter:spring-boot-starter-swagger,欢迎使用和吐槽的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot 2.3.0 发布
- 下一篇: Android Hook技术防范漫谈