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

感谢以上大佬。

相关推荐
杨丰玮4185 小时前
从零手写Java飞机躲障碍游戏|Swing绘图、鼠标跟随、计时器碰撞检测实战(五)
java·python·游戏·游戏引擎·图形渲染·动画·贴图
莫得感情 o6 小时前
踩坑 - 压测三轮后 502:一个默认 10 的连接池如何拖垮整个服务
java
Escalating_xu6 小时前
【Linux线程】线程控制全解析:终止、join/detach、cancel、线程栈与 NPTL(下篇)
java·linux·运维
2602_959960926 小时前
电商大厂Java面试实录:Spring Boot/JVM/Redis/Kafka/微服务/安全/测试全解析
java·jvm·spring boot·redis·面试题
吃饱了得干活6 小时前
限界上下文之后:微服务怎么拆、上下文怎么聊?
java·后端·架构
yngsqq6 小时前
窗体快速取消
java·开发语言
岁月如歌77867 小时前
一次讲透 Redis 缓存击穿、穿透、雪崩,从原理到实战解决方案
java·后端·架构
weixin_BYSJ19877 小时前
springboot智慧公共交通系统---附源码51123
java·javascript·spring boot·python·django·flask·php
AI多Agent协作实战派7 小时前
AI多Agent协作系统实战(四十六):改了十次规则,AI员工还是老样子——会话缓存的坑
java·spring·缓存
一知半解仙7 小时前
当机器人学会泛化,而我在写Java:一名后端开发者的2026年8月21日观察手记
java·开发语言·机器人