配置springdoc swagger开关

java 复制代码
package com.iflytek.knowledge.config;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.SpringDocConfigProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
// 通过配置开关控制是否启用 Swagger,默认开启
@ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true)
public class SwaggerConfig {
    
    @Value("${springdoc.api-docs.enabled:true}")
    private Boolean enabled;
    
    @Value("${springdoc.api-docs.version:v1.0.0}")
    private String version;
    
    @Value("${springdoc.api-docs.title:知识库管理系统 API 文档}")
    private String title;
    
    @Value("${springdoc.api-docs.description:基于 Spring Boot + MyBatis-Plus + chatdoc.xfyun.cn 的知识库/文档管理接口文档}")
    private String description;

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title(title)
                        .description(description)
                        .version(version)
                        .contact(new Contact().name("开发团队").email("dev@example.com"))
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("官方文档")
                        .url("https://tttt.tttt.cn"));
    }

    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("全部接口")
                .pathsToMatch("/**")
                .build();
    }
}

然后在 application.yml 或 application.properties 中添加配置:

YAML 格式 (application.yml):

yaml 复制代码
# SpringDoc 配置
springdoc:
  api-docs:
    enabled: true  # 是否启用 API 文档,生产环境可以设置为 false
    path: /api-docs  # API 文档路径
    version: v1.0.0  # 版本号
    title: 知识库管理系统 API 文档  # 标题
    description: 基于 Spring Boot + MyBatis-Plus + chatdoc.xfyun.cn 的知识库/文档管理接口文档  # 描述
  swagger-ui:
    enabled: true  # 是否启用 Swagger UI
    path: /swagger-ui.html  # Swagger UI 路径
    operations-sorter: method  # 接口排序方式
    tags-sorter: alpha  # 标签排序方式
  show-actuator: false  # 是否显示 Actuator 端点
  default-consumes-media-type: application/json  # 默认请求类型
  default-produces-media-type: application/json  # 默认响应类型

# 或者针对不同环境配置
# 开发环境开启
---
spring:
  config:
    activate:
      on-profile: dev
springdoc:
  api-docs:
    enabled: true
  swagger-ui:
    enabled: true

# 生产环境关闭
---
spring:
  config:
    activate:
      on-profile: prod
springdoc:
  api-docs:
    enabled: false
  swagger-ui:
    enabled: false

Properties 格式 (application.properties):

properties 复制代码
# SpringDoc 配置
springdoc.api-docs.enabled=true
springdoc.api-docs.path=/api-docs
springdoc.api-docs.version=v1.0.0
springdoc.api-docs.title=知识库管理系统 API 文档
springdoc.api-docs.description=基于 Spring Boot + MyBatis-Plus + chatdoc.xfyun.cn 的知识库/文档管理接口文档
springdoc.swagger-ui.enabled=true
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.swagger-ui.operations-sorter=method
springdoc.swagger-ui.tags-sorter=alpha
springdoc.show-actuator=false
springdoc.default-consumes-media-type=application/json
springdoc.default-produces-media-type=application/json
相关推荐
文丑颜不良啊2 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发2 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
天天被压力2 小时前
【A股分钟级高频数据完全手册 #07】北交所历史分钟K线接口:京市Pro版分时批量
java·人工智能·python
天天被压力2 小时前
【A股分钟级高频数据完全手册 #08】股票日、分时数据接口对比:普通分时与Pro版差异实测
java·人工智能·python
特立独行的猫A3 小时前
AI 时代,我为什么推荐用 cj-tauri 开发桌面应用
java·后端·python
金銀銅鐵3 小时前
从一个类名到一张类图:用反射自动生成 PlantUML
java·后端·uml
nice_lcj5203 小时前
Java 并发容器入门:ConcurrentHashMap、CopyOnWriteArrayList、阻塞队列
java·面试·并发编程·阻塞队列·concurrenthashmap·copyonwritearraylist
微小的鱼3 小时前
Linux的渲染与显示系统链路
android·java·linux
若亦_4 小时前
Java中关于this的三种用法
java·开发语言·intellij-idea
山甫aa4 小时前
锁一篇搞定:乐观锁、悲观锁、CAS、数据库锁与分布式锁【从零开始的 Web 后端学习】
java·数据库·分布式·后端·学习·web