前置条件
springBoot版本大于2.4
引入依赖(以openapi3为例)
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
OpenAPI3规范Knife4j底层依赖了springdoc-openapi项目,需要项目中有该项目依赖
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<!--版本需和knife4j-openapi3-spring-boot-starter中依赖的版本相同-->
<version>1.6.9</version>
</dependency>
配置项
# springdoc-openapi项目配置
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
api-docs:
path: /v3/api-docs
group-configs:
- group: 'default'
paths-to-match: '/**'
# 注意包路径分割符是点,直接在IDEA中copy的路径是斜杠会导致配置不生效
packages-to-scan: com.xiaominfo.knife4j.demo.web
# knife4j的增强配置,不需要增强可以不配
knife4j:
enable: true
setting:
language: zh_cn
引入之后,其余的配置可完全参考springdoc-openapi
Knife4j更多增强配置明细,参考文档
使用
项目启动后访问:http://ip:port/doc.html