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

如何在Maven的openapi-generator-maven-plugin中自定义ApiClient类名?

自定义OpenAPI生成的ApiClient类名方案

一、直接通过插件配置实现(最简单)

对于Java Feign库的生成器,openapi-generator提供了apiClientName配置参数,可直接指定ApiClient的类名。只需在configOptions中添加该配置即可:

修改后的pom.xml插件配置如下:

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${project.build.directory}/${open-specification.path}</inputSpec>
        <generatorName>java</generatorName>
        <library>feign</library>
        <generateApiTests>false</generateApiTests>
        <generateModelTests>false</generateModelTests>
        <apiPackage>com.somthing.spring.api_client.generated.api</apiPackage>
        <modelPackage>com.somthing.somthing.api_client.generated.model</modelPackage>
        <configOptions>
          <useJakartaEe>true</useJakartaEe>
          <!-- 新增配置,指定ApiClient类名 -->
          <apiClientName>HelloworldApiClient</apiClientName>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

执行mvn clean generate后,生成的客户端类会自动变为HelloworldApiClient,所有引用该类的地方(比如各API接口的默认构造方法)也会同步更新。

二、自定义模板(灵活适配复杂需求)

如果遇到生成器不支持上述配置的情况,可通过自定义模板实现:

  1. 找到openapi-generator官方模板中Java Feign对应的ApiClient.mustache文件(路径通常为modules/openapi-generator/src/main/resources/Java/feign/ApiClient.mustache)
  2. 将该文件复制到项目目录下,比如src/main/resources/openapi-templates/
  3. 打开复制后的模板,把所有ApiClient替换为HelloworldApiClient
  4. 在插件配置中添加templateDirectory指定自定义模板路径:
<configuration>
  <!-- 其他原有配置 -->
  <templateDirectory>${project.basedir}/src/main/resources/openapi-templates</templateDirectory>
</configuration>

这种方式可完全自定义生成类的结构和命名,适合需要深度定制的场景。

三、脚本替换(临时变通方案)

如果上述两种方式都无法使用,可借助Maven的Replacer插件在代码生成后批量替换类名:

  1. 在pom.xml中添加Replacer插件配置:
<plugin>
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>replacer</artifactId>
  <version>1.5.3</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>replace</goal>
      </goals>
      <configuration>
        <file>${project.build.directory}/generated-sources/openapi/src/main/java/com/somthing/java/api_client/generated/ApiClient.java</file>
        <replacements>
          <replacement>
            <token>public class ApiClient</token>
            <value>public class HelloworldApiClient</value>
          </replacement>
          <replacement>
            <token>private ApiClient</token>
            <value>private HelloworldApiClient</value>
          </replacement>
          <!-- 根据实际情况补充其他需要替换的内容 -->
        </replacements>
      </configuration>
    </execution>
  </executions>
</plugin>
  1. 确保Replacer插件在openapi-generator插件之后执行,才能替换生成后的文件。

注意:这种方式需要手动维护所有替换规则,容易遗漏引用,仅建议作为临时解决方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 06:27:11