You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Swagger API中使用自定义类作为查询参数

Swagger Codegen场景下查询参数类的定义与引用问题

我之前在Stack Overflow文章《How to accept multiple query parameters with different parameter name in one class》中学习并实现了将多个查询参数封装到单个类的方法。但当前我处于使用Swagger Codegen生成API默认代码的场景,需要基于生成的接口实现控制器逻辑。

核心问题

当前我的API文档需要逐个引用查询参数(示例代码如下),想知道如何在内部API文档文件中指定使用自定义类作为查询参数对象?

/tasks:
  get:
    parameters:
      - $ref: 'api-params.yaml#/name'
      - $ref: 'api-params.yaml#/age'
      - $ref: 'api-params.yaml#/credentials'
    responses:
      '200':
        description: OK
        content:
          application/json:
            schema:
                $ref: 'api-resp.yaml#/someResponse'
      "401":
        description: "Unauthorized"
        content: { }
      "403":
        description: "Forbidden"
        content: { }

附加疑问

  1. 能否直接在Swagger中定义模型?
  2. 是否可以像请求体那样,通过如下方式引用自定义模型作为查询参数?
schema:
  $ref: '#/components/schemas/RquestBodyPayload'

内容的提问来源于stack exchange,提问作者Vaibhav Jain

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 16:21:52