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

如何通过Gradle的org.openapi.generator插件跳过Controller类生成?

仅生成模型与API接口的配置方案(OpenAPI Generator 7.0.1)

是的,在7.0.1版本的org.openapi.generator插件中,完全可以通过配置实现仅生成模型类和API接口,不生成对应的Controller实现类。核心是在configOptions中添加专属控制属性,配合全局配置调整生成逻辑即可。

修改后的完整配置

openApiGenerate {
    generatorName = "spring"
    inputSpec = "$projectDir/src/main/api/${spec}".toString()
    outputDir = "$projectDir/build"
    apiPackage = "com.smth.api"
    modelPackage = "com.smth.model"
    configOptions.set([
            useOneOfInterfaces                  : "false",
            hideGenerationTimestamp             : "true",
            skipIfSpecIsUnchanged               : "true",
            additionalModelTypeAnnotations      : "@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor",
            generatedConstructorWithRequiredArgs: "false",
            useTags                             : "true",
            interfaceOnly                       : "true" // 新增:仅生成API接口,跳过Controller实现类
    ])
    validateSpec = false
    globalProperties.set([
            modelDocs      : "false",
            models         : "", // 生成所有模型类
            apis           : "", // 生成所有API接口
            supportingFiles: ""  // 不需要ApiUtil.java可设为空,按需调整
    ])
    typeMappings = [
            "date"   : "OffsetDateTime",
            "instant": "LocalDate"
    ]
    importMappings = [
            OffsetDateTime: "java.time.OffsetDateTime",
            LocalDate     : "java.time.LocalDate"
    ]
}

关键配置说明

  • interfaceOnly: "true":这是Spring生成器的核心控制项,开启后插件只会生成API接口(比如你提到的ValidateApi),不会再生成对应的Controller实现类(ValidateApiController)。
  • globalProperties中的models和apis:设为空字符串表示生成OpenAPI规范里定义的所有模型和接口;如果只需要生成特定的模型/接口,直接填写对应名称即可(多个用逗号分隔)。
  • supportingFiles:如果不需要ApiUtil.java这类辅助文件,直接设为空就能跳过生成,减少冗余文件。

配置完成后,build目录下只会产出com.smth.api包下的API接口和com.smth.model包下的模型类,不会出现Controller实现类。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 15:15:07