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

如何让OpenApi Generator生成以path中time命名的Spring Boot控制器?

解决OpenAPI Generator按路径分组生成Spring Boot控制器的问题

方案1:通过OpenAPI规范的tags定义实现分组

直接在OpenAPI 3.0.2的配置文件(YAML/JSON)中,给所有/v1/time/前缀的路径操作添加统一标签,生成器会自动按标签分组创建控制器:

  • 给每个/v1/time/相关的接口操作添加tags: ["Time"]
  • 这样所有带Time标签的接口会被归到TimeApi控制器类中,同时保留路径里的/v1/前缀
  • 配置示例:
paths:
  /v1/time/integrations/{some-path-variable}/some-action:
    get:
      tags: ["Time"]
      summary: 获取时间集成数据
      parameters:
        - name: some-path-variable
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: 成功响应

方案2:配置Maven插件参数强化分组逻辑

在openapi-generator-maven-plugin的配置中,开启按标签生成控制器的开关,同时指定包结构:

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.1.0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <generatorName>spring</generatorName>
        <library>spring-boot</library>
        <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
        <apiPackage>com.yourproject.api.time</apiPackage>
        <modelPackage>com.yourproject.model</modelPackage>
        <useTags>true</useTags> <!-- 核心参数:按标签生成独立控制器 -->
        <removeOperationIdPrefix>true</removeOperationIdPrefix> <!-- 优化生成的方法名 -->
      </configuration>
    </execution>
  </executions>
</plugin>
  • 针对v2版本的API,只需给对应路径添加tags: ["V2Time"],即可生成独立的V2TimeApi控制器,实现版本隔离。

方案3:自定义模板实现更灵活的分组(可选)

如果前两种方案无法满足需求,可以自定义控制器模板:

  1. 复制OpenAPI Generator默认的Spring控制器模板(api.mustache)到项目的src/main/resources/templates目录
  2. 修改模板中的类名生成逻辑,从路径中提取time部分作为类名(替换原有的V1Api逻辑)
  3. 在插件配置中指定自定义模板路径:
<configuration>
  <!-- 其他配置 -->
  <templateDirectory>src/main/resources/templates</templateDirectory>
</configuration>

内容的提问来源于stack exchange,提问作者Shivendra Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 19:31:02