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.
## 简介 使用最新版的 `Springboot 3.2.1` 搭建开发环境进行开发,调用接口时出现
渲染中...
## 简介
使用最新版的 `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.
<!-- more -->
## 原因分析
首先,这是 `Spring` 新版本导致的。为什么会出现这个问题呢?原来是 `Spring 6.1` 之后,官方加强了很多错误校验和报错提示,本文这个错也是其中之一。
<!-- 文章内嵌广告位 -->
<div class="article-ads"></div>
Spring表示:URL中的传参,必须使用 `@PathVariable` 声明用于接收的变量,如:
```java
@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](https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/requestparam.html) 会发现最后有一句话:
> 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") 这种写法。
<!-- 文章内嵌广告位 -->
<div class="article-ads"></div>
但如果不使用 `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`:
```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>
```
## 解决
<!-- 文章内嵌广告位 -->
<div class="article-ads"></div>
这种现象不知道是不是官方的BUG,但目前我发现两种解决方案:
1. 使用 `@RequestParam("name")`:
```java
// 在 name 参数前加上 @RequestParam("name")
@GetMapping("/hello")
public RespPack<?> hello(@RequestParam("name") String name) {
return null;
}
```
2. 使用 `spring-boot-starter-parent`:
```xml
<!-- 将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>
```
更信息可阅读我在 `stackoverflow` 中的提问:[stackoverflow](https://stackoverflow.com/questions/77847644/use-springboot-3-2-1spring-6-1-2-when-didnt-use-spring-boot-starter-parent/)
### maven-compiler-plugin
网友提除解决方案:父 `pom` 包含 `maven-compiler-plugin` 的配置:
```xml
<build>
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
```
这可确保使用 `-parameters` 标志编译代码,从而使参数名称在运行时可用。
如果不使用父 `pom`,也可以在自己的`pom`中直接添加插件配置来配置:
```xml
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
```
<!-- 文章内嵌广告位 -->
<div class="article-ads"></div>
END
评论
登录后查看和发表评论
前往登录