方法一:
使用步骤
1.导入 knife4j 的maven坐标
在pom.xml中添加依赖
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId><version>4.5.0</version>
</dependency>
2.在配置类中加入 knife4j 相关配置WebMvcConfiguration.java
/*** 通过knife4j生成接口文档* @return
*/@Beanpublic Docket docket() {ApiInfo apiInfo = new ApiInfoBuilder().title("项目接口文档").version("2.0").description("项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.sky.controller")).paths(PathSelectors.any()).build();return docket;}
3. 设置静态资源映射,否则接口文档页面无法访问
WebMvcConfiguration.java
/*** 设置静态资源映射* @param registry*/protected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}
4.访问:localhost:8080/doc.html
完整代码如下:
package com.qing.springboot.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;/*** 配置类,注册web层相关组件*/
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {/*** 通过knife4j生成接口文档* @return*/@Beanpublic Docket docket() {ApiInfo apiInfo = new ApiInfoBuilder().title("项目接口文档").version("2.0").description("项目接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).select().apis(RequestHandlerSelectors.basePackage("com.qing.springboot.controller")).paths(PathSelectors.any()).build();return docket;}/*** 设置静态资源映射* @param registry*/@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}
}
方法二:
1.1 Maven 依赖引入
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId><version>4.5.0</version>
</dependency>
1.2 application.yaml 添加配置
springdoc:default-flat-param-object: trueswagger-ui:path: /swagger-ui.htmltags-sorter: alphaoperations-sorter: alphaapi-docs:path: /v3/api-docsgroup-configs:- group: 'default'paths-to-match: '/**'packages-to-scan: com.abc.project
knife4j:enable: truesetting:language: zh_cn
1.3 添加配置文件
@Slf4j
@Configuration
public class SwaggerConfiguration implements ApplicationRunner {
@Value("${server.port:8080}")private String serverPort;@Value("${server.servlet.context-path:}")private String contextPath;
/*** 自定义 openAPI 个性化信息*/@Beanpublic OpenAPI openAPI() {return new OpenAPI().info(new Info() // 基本信息配置.title("xx系统") // 标题.description("描述信息") // 描述 Api 接口文档的基本信息.version("v1.0.0") // 版本// 设置 OpenAPI 文档的联系信息,包括联系人姓名为"dong",邮箱为"abc.qq.com".contact(new Contact().name("dong").email("abc.qq.com")));}
/*** 方便大家启动项目后可以直接点击链接跳转,而不用自己到浏览器输入路径*/@Overridepublic void run(ApplicationArguments args) throws Exception {log.info("API Document: http://127.0.0.1:{}{}/doc.html", serverPort, contextPath);}
}
友情提示:有些内容是可以根据自己的项目进行改写的,框架还有一些其他的功能,请自行学习。祝每次运行顺利!