You need to enable JavaScript to run this app. Camel2.25如何过滤Swagger REST端点 按系统生成专属接口文档
优惠活动
大模型
产品
解决方案
定价
更多

Camel2.25如何过滤Swagger REST端点 按系统生成专属接口文档

Camel 2.25 按对接系统生成专属Swagger文档的实现方案

方案1:按路径前缀过滤(适配现有代码结构)

你当前已经给不同系统的端点配置了system1/system2的独立路径前缀,直接通过多Swagger上下文+路径正则过滤即可实现需求,无需反复修改端点的.apiDocs()配置:

// 全局通用基础配置
restConfiguration().component("servlet")
        .enableCORS(true)
        .bindingMode(RestBindingMode.auto);

// system1专属Swagger配置
restConfiguration("system1-doc")
        .apiContextPath("/system1/api-doc")
        .apiProperty("api.title", "System1 对接接口文档")
        .apiProperty("api.version", "1.0.2")
        .apiProperty("cors", "true")
        // 正则匹配仅展示路径前缀为/system1的端点
        .apiProperty("apis.filter", "^/system1.*");

// system2专属Swagger配置
restConfiguration("system2-doc")
        .apiContextPath("/system2/api-doc")
        .apiProperty("api.title", "System2 对接接口文档")
        .apiProperty("api.version", "1.0.2")
        .apiProperty("cors", "true")
        // 正则匹配仅展示路径前缀为/system2的端点
        .apiProperty("apis.filter", "^/system2.*");

// 原有端点定义无需修改
rest("system1")
        .consumes("application/json").produces("application/json")
        .post("/example")
        .tag("system1")
        // 原有业务逻辑
        .to("...");

rest("system2")
        .consumes("application/json").produces("application/json")
        .post("/example2")
        .tag("system2")
        // 原有业务逻辑
        .to("...");

配置完成后,访问{服务部署根路径}/system1/api-doc即可获取仅包含system1端点的Swagger文档,访问{服务部署根路径}/system2/api-doc即可获取system2的专属文档,批量导出静态文档时直接请求对应地址的返回结果即可。

方案2:按Tag过滤(更灵活的分组规则)

如果存在同一路径前缀下的端点分属不同系统的场景,你已经给端点添加了tag配置,可以直接替换过滤规则实现按Tag筛选:

// 将system1的Swagger配置改为按Tag过滤
restConfiguration("system1-doc")
        .apiContextPath("/system1/api-doc")
        .apiProperty("api.title", "System1 对接接口文档")
        .apiProperty("api.version", "1.0.2")
        .apiProperty("cors", "true")
        // 仅返回Tag为system1的端点
        .apiProperty("tags.filter", "system1");

注意事项

  • Camel 2.25版本的过滤参数支持正则匹配,可根据实际的路径、Tag规则调整匹配逻辑
  • 公共端点可通过调整正则规则加入多个系统的文档中,实现多系统文档共用

内容的提问来源于stack exchange,提问作者Martin SIMON

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 15:15:04