1.引入项目依赖: <dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-spring-boot-starter</artifactId><version>4.5.0</version> </dependency>
2.新增配置文件
import io.swagger.v3.oas.models.ExternalDocumentation; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.Info; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class OpenApiConfig {@Beanpublic OpenAPI springShopOpenAPI() {return new OpenAPI()// 接口文档标题.info(new Info().title("API接口文档")// 接口文档简介.description("这是基于Knife4j OpenApi3的接口文档")// 接口文档版本.version("v1.0")// 开发者联系方式.contact(new Contact().name("xxx").email("xxxxx@qq.com"))).externalDocs(new ExternalDocumentation().description("SpringBoot基础框架").url("http://127.0.0.1:8080"));}}
3.bootstrap.yml配置文件
server:port: 8080 springdoc:api-docs:enabled: true # 是否启用接口文档 knife4j:enable: true # 是否启用 knife4j 增强,如果只是使用 knife4j 的 UI,则可以关闭basic:enable: true# Basic认证用户名username: admin# Basic认证密码password: 123456
启动后访问:localhost:8080/doc.html