目录
一、引入依赖
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
总结到此!