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

SpringBoot中如何仅从OpenAPI生成指定包下的Model类?

SpringBoot pom.xml 配置(仅生成OpenAPI Model类)

要实现仅生成Model类并指定输出路径,你需要使用openapi-generator-maven-plugin插件,以下是具体的pom.xml配置:

<build>
    <plugins>
        <!-- OpenAPI Generator 插件 -->
        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>6.6.0</version> <!-- 使用最新稳定版本 -->
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <!-- 指定你的OpenAPI定义文件路径 -->
                        <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                        <!-- 生成器类型选择spring -->
                        <generatorName>spring</generatorName>
                        <!-- 指定输出目录为src/main/java -->
                        <outputDir>${project.basedir}/src/main/java</outputDir>
                        <!-- Model类的包路径 -->
                        <modelPackage>com.service.model</modelPackage>
                        <!-- 配置仅生成Model,禁用API等其他内容 -->
                        <configOptions>
                            <generateApi>false</generateApi>
                            <generateModels>true</generateModels>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <interfaceOnly>false</interfaceOnly>
                            <skipDefaultInterface>true</skipDefaultInterface>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

关键配置说明:

  • <inputSpec>:替换为你的OpenAPI定义文件(yaml或json格式)的实际路径。
  • <outputDir>:设置为src/main/java,替代默认的target临时目录。
  • <modelPackage>:指定Model类生成后的包路径为com.service.model。
  • <configOptions>:
    • generateApi: false:禁用API接口类的生成。
    • generateModels: true:启用Model类生成。
    • generateSupportingFiles: false:禁用辅助文件(如配置类、通用异常类等)生成。
    • skipDefaultInterface: true:避免生成默认的空API接口模板。

配置完成后,执行mvn clean compile或mvn openapi-generator:generate命令即可触发Model类的生成。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 04:06:25