Spring Boot 3.2.2升级后Groovy项目参数名未指定错误修复咨询
修复Spring Boot 3.2.2 + Groovy 参数名解析报错问题
问题根源
Spring Boot 3.2.2 依赖的 Spring Framework 6.0.2 移除了通过字节码隐式获取@PathVariable/@RequestParam参数名的逻辑,必须依赖编译时生成的参数元数据才能正确解析。Groovy 项目使用 gmavenplus-plugin 时,仅配置<parameters>true</parameters>不足以覆盖存根生成阶段的参数元数据保留,导致 Spring 仍无法正确识别参数名。
具体修复步骤
1. 完善 gmavenplus-plugin 配置
Groovy 编译需先生成 Java 存根,因此要确保存根生成阶段也启用参数元数据保留,修改插件配置如下:
<plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.13.1</version> <configuration> <parameters>true</parameters> <!-- 为存根和测试存根单独开启参数保留 --> <stubs> <parameters>true</parameters> </stubs> <testStubs> <parameters>true</parameters> </testStubs> </configuration> <executions> <execution> <goals> <goal>addSources</goal> <goal>addTestSources</goal> <goal>generateStubs</goal> <goal>compile</goal> <goal>generateTestStubs</goal> <goal>compileTests</goal> <goal>removeStubs</goal> <goal>removeTestStubs</goal> </goals> </execution> </executions> </plugin>
2. 强制显式声明注解参数名
确保所有@PathVariable、@RequestParam都显式指定name或value属性,禁止依赖参数名隐式映射:
// 错误示例(Spring 6 无法自动解析参数名) @GetMapping("/users/{id}") def getUser(@PathVariable Long id) { ... } // 正确示例 @GetMapping("/users/{id}") def getUser(@PathVariable(name = "id") Long id) { ... }
3. 清理并重新编译项目
执行 Maven 清理命令,彻底移除旧的编译缓存和存根文件:
mvn clean install
4. 验证参数元数据有效性
添加 Spring 调试日志,确认参数名解析正常:
在application.properties中配置:
logging.level.org.springframework.web.method.annotation=DEBUG
启动项目后,查看日志中是否出现参数名解析成功的日志条目,比如Found @PathVariable("id")相关内容。
内容的提问来源于stack exchange,提问作者bytor99999
相关产品推荐
相关产品推荐

