访问 http://localhost:5173/admin/my-resource 路径时,返回400错误:
code: 400
msg: "参数错误: Name for argument of type [java.lang.Integer] not specified, and parameter name information not found in class file either."
这是Spring Boot参数绑定的经典问题。当Java编译时没有保留参数名信息时,Spring无法正确绑定方法参数,特别是当参数没有明确的注解或注解信息不完整时。
在 pom.xml 中添加了 -parameters 编译参数,这样Java编译时会保留参数名信息:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>8</source>
<target>8</target>
<parameters>true</parameters> <!-- 新增:保留参数名信息 -->
</configuration>
</plugin>
重要:修复后需要重新编译项目才能生效!
# 在项目根目录执行
mvn clean compile
# 或者重新打包
mvn clean package
# 然后重启admin服务
修复后,访问以下路径应该正常工作:
GET /admin/my-resource/list?page=1&pageSize=10GET /admin/my-resource/{id}PUT /admin/my-resource/{id}/audit?status=1-parameters 参数?在Java 8之前,编译后的class文件默认不包含方法参数名信息。Spring Boot在运行时需要通过参数名来绑定HTTP请求参数,如果class文件中没有参数名信息,就会出现这个错误。
使用 -parameters 编译参数(推荐)
使用 @RequestParam 明确指定参数名
使用 @RequestParam 的 value 属性
当前代码已经使用了方案2和3(所有参数都有 @RequestParam 注解),但为了确保兼容性,还是添加了 -parameters 参数。
pom.xml - Maven编译配置(已修复)service/admin/src/main/java/com/zhentao/controller/MyResourceController.java - Controller代码(无需修改)如果重新编译后问题仍然存在,请检查:
-parameters 参数已生效@RequestParam 注解修复时间: 2025-01-27
修复人: Auto