Reprinted by this article: /post/191
Introduction
Use the latest versionSpringboot 3.2.1 (I use 3.2.0)
When building a development environment for development, a strange error occurred when calling the interface. The main information of the error is as follows:
Name for argument of type [] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the ‘-parameters’ flag.
Cause analysis
First of all, this isSpring
The new version leads to it. Why does this problem occur? It turns out to beSpring 6.1
After that, the official strengthened many error checksum error notifications, and this error in this article is also one of them.
Spring means: Passing parameters in the URL must be used@PathVariable
Declare the variables used for receiving, such as:
@DeleteMapping("/employees/{employeeId}") public String deleteEmployee(@PathVariable int employeeId) { ... } @PatchMapping("/employees/{id}/{firstName}") public String patchEmployee(@PathVariable Integer id, @PathVariable String firstName) { ... }
The official statement has been emphasized@PathVariable
The use of@RequestParam
, refer to the official document @RequestParam and you will find a last sentence:
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
.Translate it roughly:
Notice
@RequestParam
The use of the sys- For example, setting its properties). By default, any simple value type (determined by BeanUtils#isSimpleProperty) and not parsed by any other parameter parser will be considered to be used@RequestParam
annotation.
According to the original text and translation, this naturally makes me think,@RequestParam
It can still be omitted.
But strangely, whenSpringboot 3.2.1
When using Maven to manage projects, if notspring-boot-starter-parent
As a parent project, the interface must be explicitly declared@RequestParam("name")
, missing onename
An error will also be reported. I clearly remember that I often omitted @RequestParam("name") in older versions of Springboot.
But if not usedspring-boot-starter-parent
As a parent project, it seems@RequestParam
It becomes annotation that cannot be omitted. When you build microservices and multi-modules, you usually don't use them.spring-boot-starter-parent
As a parent project? Or is it just me not to use it? . . . Try not to try the new version, as it will take less trouble
Error code
When there are normal parameters in the request URL, such as:http://localhost:8080/user/hello?name=zhangsan
,inname
As a parameter, yoursController
The code is roughly as follows:
@GetMapping("/hello") public RespPack<?> hello(String name) { return null; }
main:
<dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
solve
I don't know if this phenomenon is an official bug, but I have found several solutions:
- Use on parameters
@RequestParam("name")
: - use
spring-boot-starter-parent
:
<!-- Willspring-boot-starter-parentIntroduced as parent project --> <parent> <groupId></groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.1</version> <relativePath/> </parent>
maven-compiler-plugin
Netizen's removal solution: fatherpom or itself pom
Add tomaven-compiler-plugin
Configuration:
<build> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.0</version> <configuration> <parameters>true</parameters> </configuration> </plugin> </build>
This ensures use-parameters
Flags compile the code so that the parameter name is available at runtime.
This is the article about the problem of not being able to parse parameter parameters. For more related contents of springboot that cannot parse parameter parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!