Name for argument of type [java.lang.String] not … Ensure that the compiler uses the ‘-parameters’

前排提示,本文为引流文,文章内容不全,更多信息前往:oldmoon.top


简介

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

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.

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

Note that use of @RequestParam is 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作为父工程吧?还是只有我不用?。。。


更多信息: https://oldmoon.top/post/191

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>