当前位置: 首页 > news >正文

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; 
}
  • 主要pom.xml
  •   <dependencyManagement> 
          <dependencies> 
              <dependency> 
              <groupId>org.springframework.boot</groupId> 
              <artifactId>spring-boot-dependencies</artifactId> 
              <version>${boot.version}</version> 
              <type>pom</type> 
              <scope>import</scope> 
              </dependency> 
          </dependencies> 
      </dependencyManagement> 
      <dependencies> 
          <dependency> 
              <groupId>org.springframework.boot</groupId> 
              <artifactId>spring-boot-starter-web</artifactId> 
          </dependency> 
      </dependencies>
    

解决

这种现象不知道是不是官方的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标志编译代码,从而使参数名称在运行时可用。

相关文章:

  • git规范提交之commitizen conventional-changelog-cli 安装
  • 使用Python和p5.js创建的迷你游戏示例,该游戏包含多个屏幕和动画,满足在画布上显示图像、使用键盘命令移动图像
  • [GHCTF 2025]SQL??? 【sqlite注入】
  • java中过滤器
  • 反码与补码
  • 【阿里云】控制台使用指南:从创建ECS到系统诊断测评
  • (base)user1@ubuntu:~$pip install xxxx(xxxx表示第三方库) 分析
  • Centos7系统遇到的第一个问题 yum
  • 【面试】框架
  • ngx_regex_create_conf
  • Git(一)
  • leetcode hot100--动态规划【五步总纲】
  • 文本对抗样本系列的论文阅读笔记(整理合订)
  • 学习笔记11——并发编程之并发关键字
  • ArcGIS Pro中字段的新建方法与应用
  • 从新手到专家:嵌入式代码空间优化技巧
  • C++ Primer Plus第十二章课后习题总结
  • 【二分算法】-- 三种二分模板总结
  • 【实战ES】实战 Elasticsearch:快速上手与深度实践-5.1.2基于Painless脚本的日志告警
  • 空指针异常的触发
  • 多少Moreless:向世界展示现代中式家具的生活美学
  • 女生“生理期请病假要脱裤子证明”?高校回应:视频经处理后有失真等问题
  • 《五行令》《攻守占》,2个月后国博见
  • 霍步刚任辽宁沈阳市委书记
  • 就规范涉企行政执法专项行动有关问题,司法部发布解答
  • 年在沪纳税350亿人民币,这些全球头部企业表示“对上海承诺不会变”