news 2026/4/3 6:30:36

springboot3.X 无法解析parameter参数问题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
springboot3.X 无法解析parameter参数问题

本文参考转载:https://oldmoon.top/post/191

简介

使用最新版的Springboot 3.2.1(我使用3.2.0)搭建开发环境进行开发,调用接口时出现奇怪的错。报错主要信息如下:

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新版本导致的。为什么会出现这个问题呢?原来是Spring 6.1之后,官方加强了很多错误校验和报错提示,本文这个错也是其中之一。

Spring表示:URL中的传参,必须使用@PathVariable声明用于接收的变量,如:

@DeleteMapping("/employees/{employeeId}") public String deleteEmployee(@PathVariable int employeeId) { ... } @PatchMapping("/employees/{id}/{firstName}") public String patchEmployee(@PathVariable Integer id, @PathVariable String firstName) { ... }

官方说明中一直强调@PathVariable的使用,并没有提及@RequestParam,参考官方文档@RequestParam会发现最后有一句话:

Note that use of@RequestParamis optional (for example, to set its attributes). By default, any argument that is a simple value type (as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver, is treated as if it were annotated with@RequestParam.

翻译一下大概是:

注意@RequestParam的使用是可选的(例如,设置其属性)。 默认情况下,任何简单值类型(由 BeanUtils#isSimpleProperty 确定)且未由任何其他参数解析器解析的参数都将被视为使用@RequestParam注解。

根据原文及翻译,这自然让我认为,@RequestParam依然是可以省略的。

然而奇怪的是,当Springboot 3.2.1使用Maven管理项目时,如果不使用spring-boot-starter-parent作为父工程,那么接口中必须显式声明@RequestParam("name"),缺了其中的name也会报错。我清晰地记得我在旧版本的 Springboot 中经常省略 @RequestParam(“name”) 这种写法。

但如果不使用spring-boot-starter-parent作为父工程,好像@RequestParam变成了不可省略注解。大家搭建微服务和多模块时候,通常不会使用spring-boot-starter-parent作为父工程吧?还是只有我不用?。。。 还是尽量不要尝试新版本,会少踩很多坑

当请求URL中有正常参数时,如:http://localhost:8080/user/hello?name=zhangsan,其中name为一个参数,你的Controller代码大概如下所示:

java

@GetMapping("/hello") public RespPack<?> hello(String name) { return null; }

解决

这种现象不知道是不是官方的BUG,但目前我发现几种解决方案:

  1. 在参数上使用@RequestParam("name")

  2. 使用spring-boot-starter-parent

    <!-- 将spring-boot-starter-parent作为父工程在pom.xml中引入 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.1</version> <relativePath/> </parent>
  3. maven-compiler-plugin

    网友提除解决方案:父pom或本身pom中添加maven-compiler-plugin的配置:

    <build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.0</version> <configuration> <parameters>true</parameters> </configuration> </plugin> </build>

这可确保使用-parameters标志编译代码,从而使参数名称在运行时可用。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/14 13:03:17

PDF-Extract-Kit部署案例:教育机构试卷自动批改系统

PDF-Extract-Kit部署案例&#xff1a;教育机构试卷自动批改系统 1. 引言&#xff1a;智能阅卷的工程化需求 1.1 教育数字化转型中的痛点 在传统教育场景中&#xff0c;教师批改试卷是一项耗时且重复性高的工作。尤其在大型考试或日常测验中&#xff0c;面对成百上千份手写或…

作者头像 李华
网站建设 2026/4/1 0:55:46

STM32调试接口初始化失败在Keil5中的应对策略

STM32调试连不上&#xff1f;Keil5初始化失败的根源与破局之道你有没有遇到过这样的场景&#xff1a;开发板焊好了&#xff0c;ST-Link也插上了&#xff0c;Keil5一点“Start Debug”&#xff0c;结果弹窗直接甩出一句冷冰冰的提示&#xff1a;Cannot access target. Shutting …

作者头像 李华
网站建设 2026/4/3 4:59:03

no stlink delected时JTAG接口状态检查指南

当“no stlink delected”时&#xff1a;JTAG/SWD连接失败的深度排查与实战指南在嵌入式开发中&#xff0c;最令人抓狂的瞬间之一莫过于——代码写完、编译通过、点击下载&#xff0c;结果弹出一行红字提示&#xff1a;“no stlink delected”。别急着换线、换板、甚至怀疑人生…

作者头像 李华
网站建设 2026/3/18 0:18:51

PDF-Extract-Kit摘要生成:自动生成文档摘要

PDF-Extract-Kit摘要生成&#xff1a;自动生成文档摘要 1. 引言&#xff1a;智能PDF内容提取的工程实践需求 在科研、教育和企业办公场景中&#xff0c;大量知识以PDF格式沉淀。传统手动摘录方式效率低下&#xff0c;尤其面对包含复杂公式、表格和图文混排的学术论文时&#…

作者头像 李华
网站建设 2026/4/3 1:25:30

PDF-Extract-Kit性能优化:GPU资源利用率提升技巧

PDF-Extract-Kit性能优化&#xff1a;GPU资源利用率提升技巧 1. 背景与挑战 1.1 PDF-Extract-Kit工具箱简介 PDF-Extract-Kit 是由开发者“科哥”基于深度学习技术二次开发构建的一款PDF智能内容提取工具箱&#xff0c;旨在解决学术论文、技术文档、扫描件等复杂PDF文件中关…

作者头像 李华