在OpenAPI Spring生成器中能否用@GetMapping替代@RequestMapping?
使用OpenAPI Generator Gradle插件生成带具体HTTP方法注解的Spring接口
当然可以生成@GetMapping、@PostMapping这类具体的HTTP方法注解,核心是调整插件的配置参数,具体实现方式如下:
配置关键参数
在Gradle的openApiGenerate任务中,需要在additionalProperties里添加对应配置:- 较新版本的插件(6.x及以上):设置
useSpringController = "true" - 旧版本插件:设置
useRequestMappingMethod = "true"
同时确保开启interfaceOnly(如果只生成接口)和对应Spring版本的参数(比如useSpringBoot3 = "true")。
- 较新版本的插件(6.x及以上):设置
示例Gradle配置
openApiGenerate { generatorName = "spring" inputSpec = "$rootDir/src/main/resources/openapi.yaml".toString() outputDir = "$buildDir/generated".toString() apiPackage = "com.example.api" modelPackage = "com.example.model" additionalProperties = [ interfaceOnly: "true", useSpringBoot3: "true", useSpringController: "true" ] }重要前提
你的OpenAPI规范文件(yaml/json)必须明确定义每个接口的HTTP方法(比如get、post、put),插件是根据规范里的方法定义来生成对应注解的。如果规范里没指定具体方法,插件还是会默认生成@RequestMapping。
内容的提问来源于stack exchange,提问作者Robert Strauch
相关产品推荐
相关产品推荐

