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
Afterwards, 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 was not mentioned@RequestParam
, refer to the official documentation@RequestParamYou 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 is optional (for example, setting its properties). By default, any parameters that are 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.
However, strangely, whenSpringboot 3.2.1
When using Maven to manage projects, if notspring-boot-starter-parent
As a parent project, then the interface must be explicitly declared.@RequestParam("name")
, there is nothing in itname
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 has become annotations 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? . . . It is better to 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 two 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> <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 end of this article about the solution to the problem of SpringBoot's inability to parse parameter parameters. For more related content related to SpringBoot's inability to parse parameter, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!