当前位置: 首页 > news >正文

Knife4j 接口文档使用流程分析

Knife4j 接口文档使用是我们常用的工具,今天我们在springBoot框架中分享一下。

Knife4j 基于 Swagger 规范开发,本质上是对 Swagger 的二次封装,通过优化 UI 和扩展功能提升开发体验‌12。例如,Knife4j 的前身是 swagger-bootstrap-ui,专为 Java 开发者设计‌。

Swagger 作为通用规范(如 OpenAPI),适用于多语言场景;而 Knife4j 聚焦于 Java 生态,解决 Spring Boot/Cloud 项目中 Swagger 的易用性问题‌。

Knife4j 的版本与 Swagger 规范紧密关联。例如,Knife4j 的 OpenAPI 3 版本基于 SpringDoc 实现,兼容 Spring Boot 3,而旧版本则基于原生 Swagger(OpenAPI 2)

1、pom文件引用

   <dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.2</version>
</dependency>

2、Swagger + Knife4j 配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j  // 启用 Knife4j 增强功能
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 指定 Controller 扫描包路径(根据实际项目调整)
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API 文档标题")
                .description("API 接口详细描述")
                .version("1.0.0")
                .contact(new Contact("开发者名称", "https://example.com", "contact@example.com"))
                .build();
    }
}

3、调整 Spring Boot 配置

application.yml 中添加以下配置,优化文档展示及避免静态资源拦截:

knife4j:
  enable: true  # 开启 Knife4j(默认已开启,可省略)
  production: false  # 生产环境建议设为 true 以禁用文档

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher  # 解决 Spring Boot 2.6+ 与 Swagger 兼容性问题

如果部署2.6+版本,那么需要引用Swagger jar,比如:

       <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

4、启动后访问

http://localhost:8080/doc.html

  • 默认 UI 路径‌:/doc.html(Knife4j 增强文档)
  • 原生 Swagger 路径‌:/swagger-ui.html

5、生产环境屏蔽

knife4j:
  production: true  # 禁用文档访问

6、使用注意:

1)请求和返回对象一定是自定义的POJO,否则文档上不显示参数详细信息。如果是继承了map或者其他原生的类,就显示不出来信息。

7、

8、

http://www.dtcms.com/a/99196.html

相关文章:

  • 7.3 主成分分析(PCA)
  • Python切片中的步长秘密
  • Python 序列构成的数组(切片)
  • sqli-labs靶场 less 10
  • prometheus+grafana监控虚拟机实操
  • Windows 11 VS Code C/C++ 开发环境搭建——一种尽量“绿色”的方法
  • defconfig配置宏的规则
  • C. Assembly via Minimums
  • 一种C# Winform的UI处理
  • Python第六章18:数据容器的通用操作
  • 简单ELK框架搭建
  • 为pip设置国内镜像源
  • Android Jetpack学习总结(源码级理解)
  • 明达IOT 平台助推纺织龙头实现智能管理
  • 动态规划篇(数位统计DP)
  • 用空闲时间做了一个小程序-二维码生成器
  • 【安全】nginx防止host头攻击
  • c++弱指针实现原理
  • Python小练习系列 Vol.5:数独求解(经典回溯 + 剪枝)
  • Linux之基础知识
  • 深度学习处理时间序列(5)
  • 《新凯来:半导体设备制造领域的“国家队”》
  • 【愚公系列】《高效使用DeepSeek》039-政务工作辅助
  • LeetCode 2360.图中的最长环:一步一打卡(不撞南墙不回头) - 通过故事讲道理
  • Redis延时队列在订单超时未报到场景的应用分享
  • 【数据结构】二叉树 — 经典OJ面试题剖析!!!
  • 关于 websocket协议的理解
  • 001 - 前缀和算法:从原理到实战,一文讲透区间和问题
  • 谈谈Minor GC、Major GC和Full GC
  • Java——数组