SpringBoot3 当前最新版knife4j openapi3 集成方案

目录

一、引入依赖

二、配置YML

三、自定义Properties

四、启动项配置Properties

五、配置Config

六、效果展示

七、相关最新的注解


一、引入依赖

XML 复制代码
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
    <version>4.5.0</version>
</dependency>

二、配置YML

XML 复制代码
# 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: '/**'
      packages-to-scan: com.xloda.xxx.controller # controller package
    - group: 'iam-api'
      paths-to-match: '/**'
      packages-to-scan: com.xloda.iam.controller # controller package
  api:
    title: AIM Service API
    version: 1.0.0
    description: API for Identity and Access Management Service
    contact:
      name: Dragon Wu
      email:
      url:
# knife4j的增强配置,不需要增强可以不配
knife4j:
  enable: true
  setting:
    language: zh_cn

生产环境需要配:

XML 复制代码
# 生产环境:关闭Knife4j核心功能
knife4j:
  enable: false
# 生产环境:关闭SpringDoc接口文档生成(双重保障,避免漏关)
springdoc:
  api-docs:
    enabled: false
  swagger-ui:
    enabled: false

三、自定义Properties

java 复制代码
/**
 * @author Dragon Wu
 * @created 2026/2/4 15:05
 * @description OpenAPI的自定义配置属性
 */
package com.xloda.iam.config.properties;

import io.swagger.v3.oas.models.info.Contact;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "springdoc.api")
public class OpenApiProperties {
    private String title = "API title";
    private String description = "API description";
    private String version = "1.0.0";
    private Contact contact = new Contact();
}

四、启动项配置Properties

java 复制代码
@SpringBootApplication
// 显式激活配置属性类,Spring自动将其注册为容器Bean,无需加@Component
@EnableConfigurationProperties(OpenApiProperties.class)
public class IamApplication {
    // ......
}

// 显式激活配置属性类,Spring自动将其注册为容器Bean,无需加@Component

@EnableConfigurationProperties(OpenApiProperties.class)

五、配置Config

java 复制代码
/**
 * @author Dragon Wu
 * @created 2026/02/04 15:37
 * @description Knife4j整合Swagger3 Api接口文档配置类
 */
package com.xloda.iam.config;

import com.xloda.iam.config.properties.OpenApiProperties;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenApiConfig {

    private final OpenApiProperties openApiProperties;

    public OpenApiConfig(OpenApiProperties openApiProperties) {
        this.openApiProperties = openApiProperties;
    }

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title(openApiProperties.getTitle())
                        .description(openApiProperties.getDescription())
                        .version(openApiProperties.getVersion())
                        .contact(openApiProperties.getContact())
                );
    }
}

六、效果展示

这样集成维护起来很方便!

七、相关最新的注解

https://blog.csdn.net/qq_50909707/article/details/157732549

总结到此!

相关推荐
星辰徐哥2 小时前
Spring Boot 微服务架构设计与实现
spring boot·后端·微服务
星辰徐哥2 小时前
Spring Boot 数据导入导出与报表生成
spring boot·后端·ui
明夜之约2 小时前
Spring Boot 自动装配源码
java·spring boot·后端
Leaton Lee2 小时前
Spring Boot分层架构详解:从Controller到Service再到Mapper的完整流程
java·spring boot·后端·架构
Micro麦可乐2 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统
Jinkxs2 小时前
Resilience4j- 与 Spring Boot 快速集成:自动配置与基础注解使用
java·spring boot·后端
毕设源码_郑学姐2 小时前
计算机毕业设计springboot网络相册设计与实现 基于Spring Boot框架的在线相册管理系统开发与应用 Spring Boot驱动的网络影集设计与实践
spring boot·后端·课程设计
辣机小司2 小时前
【踩坑记录:Spring Boot 配置文件读取值不一致?警惕 YAML 的“八进制陷阱”与 SnakeYAML 版本之谜】
java·spring boot·后端·yaml·踩坑记录
一条小锦吕*2 小时前
基于Spring Boot + 数据可视化 + 协同过滤算法的推荐系统设计与实现(源码+论文+部署全讲解)
spring boot·算法·信息可视化
Jinkxs2 小时前
Prometheus - 监控微服务:Spring Boot 应用指标暴露与监控
spring boot·微服务·prometheus