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

如何配置OpenAPI Generator Maven插件生成OAS指定类名的POJO

解决OpenAPI Generator生成POJO类名自动添加前缀的问题

问题场景

使用org.openapitools的openapi-generator-maven-plugin(6.6.0版本)基于OAS YAML文件生成Java POJO类时,生成的类名会被自动添加前缀。例如OAS中定义的schema名称为ApiResponse,生成后的类名却变成了ModelApiResponse,需要调整插件配置,使生成的类名与OAS中指定的名称完全一致。

当前插件配置

<plugin>
     <groupId>org.openapitools</groupId>
     <artifactId>openapi-generator-maven-plugin</artifactId>
     <version>6.6.0</version>
     <executions>
        <execution>
            <id>target-spec</id>
               <goals>
               <goal>generate</goal>
               </goals>
          <configuration>
            <inputSpec>${project.basedir}/src/main/resources/petstore.yaml</inputSpec>
            <generatorName>spring</generatorName>
            <modelPackage>com.petstorespringapi.stub.target</modelPackage>
            <generateApis>false</generateApis>
            <generateSupportingFiles>false</generateSupportingFiles>
            <generateApiDocumentation>false</generateApiDocumentation>
         </configuration>
        </execution>
   </executions>
</plugin>

修改后的插件配置

在插件的<configuration>节点中添加modelNamePrefix配置项并设置为空值,即可取消默认的Model前缀:

<plugin>
     <groupId>org.openapitools</groupId>
     <artifactId>openapi-generator-maven-plugin</artifactId>
     <version>6.6.0</version>
     <executions>
        <execution>
            <id>target-spec</id>
               <goals>
               <goal>generate</goal>
               </goals>
          <configuration>
            <inputSpec>${project.basedir}/src/main/resources/petstore.yaml</inputSpec>
            <generatorName>spring</generatorName>
            <modelPackage>com.petstorespringapi.stub.target</modelPackage>
            <!-- 取消模型类的默认前缀 -->
            <modelNamePrefix></modelNamePrefix>
            <generateApis>false</generateApis>
            <generateSupportingFiles>false</generateSupportingFiles>
            <generateApiDocumentation>false</generateApiDocumentation>
         </configuration>
        </execution>
   </executions>
</plugin>

也可以通过configOptions节点配置,效果一致:

<plugin>
     <groupId>org.openapitools</groupId>
     <artifactId>openapi-generator-maven-plugin</artifactId>
     <version>6.6.0</version>
     <executions>
        <execution>
            <id>target-spec</id>
               <goals>
               <goal>generate</goal>
               </goals>
          <configuration>
            <inputSpec>${project.basedir}/src/main/resources/petstore.yaml</inputSpec>
            <generatorName>spring</generatorName>
            <modelPackage>com.petstorespringapi.stub.target</modelPackage>
            <configOptions>
                <!-- 取消模型类的默认前缀 -->
                <modelNamePrefix></modelNamePrefix>
            </configOptions>
            <generateApis>false</generateApis>
            <generateSupportingFiles>false</generateSupportingFiles>
            <generateApiDocumentation>false</generateApiDocumentation>
         </configuration>
        </execution>
   </executions>
</plugin>

效果验证

修改配置后重新执行生成命令,原ModelApiResponse会生成为与OAS中一致的ApiResponse类:

/**
 * ApiResponse
 */

@JsonTypeName("ApiResponse")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-08-12T11:49:39.983257+02:00[Europe/Copenhagen]")
public class ApiResponse {}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 10:25:31