springMvc:Request processing failed: java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflectio......
异常描述:
Request processing failed: java.lang.IllegalArgumentException: 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.
请求处理失败:java.lang.IllegalArgumentException:未指定 [java.lang.String] 类型的参数的名称,并且无法通过反射获得参数名称信息。确保编译器使用“-parameters”标志。
异常截图:
代码块
//@RestController @Controller @RequestMapping("/param" ) public class ParamController { //直接接收/param/data?name=root&age=18 @RequestMapping("/data") @ResponseBody public String data(String name) { int age=18; System.out.println("name" + name + ",age" + age); return "name:" + name + ",age:" + age; } @GetMapping("/{id}") public String getId(@PathVariable String id) { return "id: " + id; } }
解决方案
add "-parameters" to compile option.
问题原因:
Compile Option "-parameters"
I found this because Compile Option "-parameters" is disabled.
因为编译选项“-参数”被禁用。
-parameters
Generates metadata for reflection on method parameters. Stores formal parameter names of constructors and methods in the generated class file so that the method
java.lang.reflect.Executable.getParameters
from the Reflection API can retrieve them.
为方法参数的反射生成元数据。在生成的类文件中存储构造函数和方法的正式参数名,以便来自反射API的方法java.lang.reflect.Executable.getParameters可以检索它们
方法二:
添加 pom插件设置
<parameters>true</parameters>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
</plugins>
</build>
tips:可能需要maven操作clean,compile来重建
问题原因:
这可确保使用 -parameters
标志编译代码,从而使参数名称在运行时可用。
如果不使用父 pom
,也可以在自己的pom
中直接添加插件配置来配置:
参考:
https://youtrack.jetbrains.com/issue/IDEA-339211/Parameter-Name-Reflection-Fails-in-IntelliJ-Builds-with-Spring-Boot-3
https://oldmoon.top/post/191#maven-compiler-plugin
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?