简介
使用最新版 Spring Boot 3.2.x 搭建开发环境时,调用接口可能出现参数解析错误。报错信息如下:
Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
原因分析
Spring 6.1 之后增强了错误校验。虽然官方文档提到 @RequestParam 对于简单类型默认可选,但在特定构建配置下(如不使用 spring-boot-starter-parent),编译器可能未保留参数名元数据。
错误示例
Controller 代码如下:
@GetMapping("/hello")
public RespPack<?> hello(String name) {
return null;
}
请求 URL:http://localhost:8080/user/hello?name=zhangsan
解决方案
1. 显式声明参数注解
在参数上添加 @RequestParam("name") 明确指定参数名。
2. 配置 Maven Compiler Plugin
在 pom.xml 中添加以下配置,确保编译时使用 -parameters 标志:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.0</version>
<configuration>
<parameters>true</parameters>

