1. 环境准备
1.1 确认 Spring Boot 版本
确保使用 Spring Boot 2.x 版本(推荐 2.5.x - 2.7.x)
Spring Boot 2.x 集成 Knife4j OpenAPI 3 需完成环境确认、依赖配置、类设置及注解使用。主要步骤包括确保 Spring Boot 版本兼容性,在 pom.xml 中添加 knife4j-openapi3-spring-boot-starter 依赖并移除旧版 Swagger 依赖。创建 SwaggerConfig 配置类启用 Knife4j 并定义 API 信息,可选配置 WebMvc 解决静态资源问题。在 application.yml 中开启 Knife4j 并设置语言为中文。控制器层使用 @Tag、@Operation 等 OpenAPI3 注解,DTO 层使用 @Schema 注解。安全框架需放行 doc.html 等路径。最终通过 localhost:8080/doc.html 访问文档。常见问题包括页面无法访问、接口不显示及静态资源加载失败,可通过检查依赖版本、注解及路径匹配策略解决。
确保使用 Spring Boot 2.x 版本(推荐 2.5.x - 2.7.x)
在 pom.xml 中添加:
<properties>
<knife4j.version>4.3.0</knife4j.version>
</properties>
<dependencies>
<!-- Knife4j OpenAPI3 增强依赖 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
</dependencies>
确保移除以下旧依赖:
<!-- 需要移除的依赖 -->
<!-- <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency> -->
注意:只保留 Knife4j 依赖,其他的 swagger 相关依赖不要保留,否则会出现冲突。
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.Contact;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
@Configuration
@EnableKnife4j // 启用 Knife4j 增强功能
@Slf4j
public class SwaggerConfig {
@Bean
public OpenAPI customOpenAPI() {
log.info("=== 初始化 Knife4j OpenAPI3 配置 ===");
return new OpenAPI().info(apiInfo());
}
private Info apiInfo() {
return new Info()
.title("项目名称 API 文档")
.description("项目描述信息")
.termsOfService("http://localhost:8080/")
.contact(new Contact()
.name("开发团队")
.url("http://localhost:8080/")
.email("[email protected]"))
.version("1.0");
}
@PostConstruct
public void init() {
log.info("=== Knife4j OpenAPI3 配置初始化完成 ===");
log.info("文档地址:http://localhost:8080/doc.html");
log.info("OpenAPI JSON: http://localhost:8080/v3/api-docs");
}
}
package com.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 确保 Knife4j 的静态资源能够正常访问
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
若访问文档地址 http://localhost:8080/doc.html 遇到 Knife4j 文档请求异常的错误提示,可以添加 WebMvc 配置来解决。
# Spring 配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher # Spring Boot 2.6+ 必须配置
# Knife4j 配置
knife4j:
enable: true
setting:
language: zh-CN # 中文界面
# Spring 配置
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
# Knife4j 配置
knife4j.enable=true
knife4j.setting.language=zh-CN
新版 OpenAPI3 注解
| OpenAPI3 注解 | 说明 | |
|---|---|---|
@Tag | 控制器分类 | |
@Operation | 接口操作 | |
@Parameter | 参数说明 | |
@ApiResponse | 响应说明 |
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户注册、登录、信息管理等接口")
public class UserController {
@PostMapping("/login")
@Operation(summary = "用户登录", description = "通过用户名和密码登录系统")
public Result<LoginVO> login(
@Parameter(description = "登录参数", required = true)
@Valid
@RequestBody LoginDTO loginDTO) {
// 业务逻辑
}
}
新版 OpenAPI3 注解
| OpenAPI3 注解 | 说明 | |
|---|---|---|
@Schema | 模型说明 | |
@Schema | 属性说明 |
@Data
@Schema(description = "用户登录请求参数")
public class LoginDTO {
@Schema(description = "用户名", example = "admin", required = true)
@NotBlank(message = "用户名不能为空")
private String username;
@Schema(description = "密码", example = "123456", required = true)
@NotBlank(message = "密码不能为空")
private String password;
}
@Data
@Schema(description = "通用返回结果")
public class Result<T> {
@Schema(description = "状态码", example = "200")
private Integer code;
@Schema(description = "提示信息", example = "成功")
private String msg;
@Schema(description = "返回数据")
private T data;
}
在安全过滤器中放行 Knife4j 相关路径:
// 在 JWT 过滤器或安全配置中
private boolean isPublicUri(String uri) {
return uri.startsWith("/doc.html") || uri.startsWith("/webjars/")
|| uri.startsWith("/v3/api-docs") || uri.startsWith("/favicon.ico")
|| uri.equals("/") || uri.startsWith("/swagger-resources");
}
@EnableKnife4j 注解已添加ant_path_matcher 配置| Spring Boot 版本 | 推荐 Knife4j 版本 | 说明 |
|---|---|---|
| 2.5.x | 4.1.0 - 4.3.0 | 稳定兼容 |
| 2.6.x | 4.1.0 - 4.3.0 | 需配置 pathmatch |
| 2.7.x | 4.3.0 | 最新稳定 |

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online