Swagger3.0 官方已經有了自己的 Spring Boot Starter,只需要添加一個 jar 包即可(SpringBoot 版本 2.3.6.RELEASE,低版本的SpringBoot要處理很多兼容性的問題)。。
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
需要引用這2個包,springfox-boot-starter就是Swagger的Starter,knife4j-spring-boot-starter是Swagger-Bootstrap-UI的最新版本
Swagger-Bootstrap-UI的中文文檔地址:https://doc.xiaominfo.com/ 開源地址:https://github.com/xiaoymin/Swagger-Bootstrap-UI
Swagger的注解和配置都是舊版本保持一致。
配置完之后,啟動項目,訪問地址:http://ip:port/doc.html
相關文檔:https://mp.weixin.qq.com/s/-yTe0N5mwN2JJyPg-MOSVQ
https://mp.weixin.qq.com/s/Su33QhBRKnzwuNgTdriaTA
注意:以上兩個Starter包中引用的swagger-model、swagger-core、swagger-annotations 的版本是1.5.20和1.5.22版本,在訪問頁面時,還會出現空字段報錯的問題。建議手動在maven中剔除,更新1.6.1以上版本的jar包。