图片预览

滚轮缩放 · 拖动移动 · Esc 关闭

Skip to content

OpenAPI 文档配置 ​

当前实现内容可在当前源码中定位并复现
适用范围
muotai-common-swagger 的接入与维护
阅读对象
后端开发、测试和运维
源码基线
muotai-backend 提交 a08dae999(muotai-common 5.8.0)
源文件
muotai-backend/muotai-common/muotai-common-swagger/
核对日期
2026-11-28

作用与提供的能力 ​

muotai-common-swagger 整合 Springdoc OpenAPI(2.8.5)与服务元数据配置,提供 @EnableOpenApi 注解一站式装配网关路由前缀、微服务聚合元信息、OAuth2 调试鉴权方案以及文档扫描路径控制。

接入与使用 ​

在业务模块的 pom.xml 中加入 com.aroundix:muotai-common-swagger(同仓库子模块沿用父 POM 版本):

  1. 在应用启动类标注 @EnableOpenApi("路由前缀"),按部署形态设置 isMicro(微服务聚合模式);该注解同时加载模块内置默认配置 openapi-config.yaml。
  2. 在应用配置中按需设置 swagger.enabled、swagger.title、description、base-package、base-path、exclude-path。
  3. 业务 Controller 可使用标准 OpenAPI 3 注解(@Tag、@Operation、@Parameter、@Schema)补充接口契约说明。

最小示例 ​

java
@EnableOpenApi("billing")
@SpringBootApplication
public class MuotaiBillingApplication { }
yaml
swagger:
  enabled: true # 生产环境关闭文档暴露请设为 false
  title: 墨台计费服务 API

文档生成、网关聚合与访问核对 ​

  • OpenAPI 定义入口:最终生成的规范以目标服务运行期的 /v3/api-docs 为准(若服务配置了 server.servlet.context-path 或反向代理前缀,需拼接实际前缀访问)。
  • 网关聚合对齐:微服务部署时,@EnableOpenApi 的路由前缀必须与网关路由路径及 OAuth2 认证地址保持一致,且网关与各业务服务网络互通,在线调试才可正常发包。

配置与装配速查 ​

项目当前源码入口
装配方式启动类 @EnableOpenApi(加载元数据配置与 openapi-config.yaml)
关键配置项swagger.enabled、swagger.title、description、base-package、base-path、exclude-path
外部前提Springdoc OpenAPI 运行环境;网关聚合需配置路由与访问控制

代码组织 ​

  • swagger/annotation/:@EnableOpenApi 启用注解。
  • swagger/config/:OpenAPI 定义注册与服务元数据组装。
  • swagger/support/:属性类定义。
  • src/main/resources/:默认 openapi-config.yaml。

验证与排障 ​

在 muotai-backend 根目录运行编译校验:

bash
mvn -B -pl :muotai-common-swagger -am test-compile -DskipTests

运行期验收步骤

  1. 开发/测试环境访问服务 /v3/api-docs,确认文档标题、扫描到的业务路径、Server 前缀及鉴权 Scheme 正确。
  2. 将 swagger.enabled 设为 false 重启,确认文档定义不再注册或暴露。

源码实测注意与生产安全红线

  1. 配置键名以 swagger.enabled 为准:源码注释中偶见 swagger.enable 笔误,但实际 Spring 条件判断绑定的是 swagger.enabled。
  2. 禁止在公网生产环境暴露文档:OpenAPI JSON 会暴露内部参数结构与调试端点,生产环境务必关闭 swagger.enabled=false,同时在网关与安全策略层拦截 /v3/api-docs 及 Swagger UI 路径。

源码入口:muotai-backend/muotai-common/muotai-common-swagger/src/main/java/com/aroundix/muotai/common/swagger/annotation/EnableOpenApi.java。相关上下游见公共组件总览。