SpringMVC新版本踩坑[已解决]

问题:

在使用最新版本springMVC做项目部署时,浏览器反复500,如下图:

异常描述:

类型异常报告

消息Request processing failed: java.lang.IllegalArgumentException: Name for argument of type int not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.

描述服务器遇到一个意外的情况,阻止它完成请求。

例外情况

jakarta.servlet.ServletException: Request processing failed: java.lang.IllegalArgumentException: Name for argument of type int not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.

根本原因。

java.lang.IllegalArgumentException: Name for argument of type int not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.

问题出现原因:新版本Spring调整了参数

找了一晚上问题所在,发现可能是新版本调整了参数,而spring会自动帮助设置,导致编译时选项"-参数"被禁用。也就是错误信息中的最后提示:Ensure that the compiler uses the '-parameters' flag.

尝试一,在idea编译器中设置指定参数,启用 -parameters 编译器标志:

根据提示,想着试下在编译时做一个配置:

但是在尝试后并未发现有作用,于是继续寻找其他解决方法。

尝试二,在项目pom.xml文件中配置插件:

复制代码
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
                <configuration>
                    <parameters>true</parameters>
                    <source>22</source>
                    <target>22</target>
                    <encoding>UTF-8</encoding>
                    <!-- 启用 -parameters 编译器标志 -->
                    <compilerArgument>-parameters</compilerArgument>
                </configuration>
            </plugin>

        </plugins>
    </build>

大部分场景这个设置生效的,但是进行测试后,发现仍然没有用。

尝试三,给参数注解@PathVariable加上value属性:

原代码:

复制代码
public Type test(@PathVariable int var1, @PathVariable int var2){
        ...
        return type;
    }

修改后代码:

复制代码
?
public Type test(@PathVariable(value ="var1") int var1, @PathVariable(value ="var2") int var2){
        ...
        return type;
    }

?

尝试测试,终于看到了久违的200:

至此,终于在各种尝试中解决了问题。

总结:在做参数传递时,需要多留心@PathVariable注解的使用,有时严格按照其使用方法也许是一个好的习惯。

参考文章:升级springboot3.2.0报Name for argument of type java.lang.String not specified, and parameter name inf-CSDN博客

【已解决】java.lang.IllegalArgumentException: Name for argument of type java.lang.Integer not specified-CSDN博客

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... - 困到很想醒 - 博客园

感谢以上大佬。

相关推荐
我就是全世界14 分钟前
OpenCode Go 套餐划算吗:先看你用哪个模型
开发语言·后端·golang
PedroQue991 小时前
0.8.2 新增 backFallback 配置,无 tabbar 页返回无忧
前端·uni-app
淡漠的蓝精灵1 小时前
web.py:一个简单到极致的 Python Web 框架
开发语言·前端·python·其他
乌暮1 小时前
请求进门先安检:Spring 拦截器与统一功能实战指南
java·开发语言·后端·学习·spring
学长毕设1 小时前
基于SpringBoot的烘焙商品商品系统-附源码
java·spring boot·后端
好的佩奇1 小时前
蓝牙之无线电与通信距离
android·flutter·蓝牙
开开心心就好1 小时前
竖式除法出题工具,整除余数混合模式
运维·服务器·前端·javascript·ocr·perl·1024程序员节
起个名字费劲死了2 小时前
Halcon学习一:骰子识别
前端·python·学习
大江东去浪淘尽千古风流人物2 小时前
【HexVIO】DSP卸载双目惯性VIO:Hexagon向量化前端与单帧融合调度深度解析
前端·状态模式·slam·dsp·vio·hexagon·移动端加速
付威20232 小时前
Durable Execution 怎样设计:从运行记录到中断恢复
后端