I used springboot3.2.1
and maven3.9.0
to create project, But i didn't used spring-boot-starter-parent
, Instead use spring-boot-dependencies
, I just want to manager my dependency, when i request a simple api, my server throw exception:
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.
.
api request url : http://localhost:8080/user/hello?name=ddd
Error Java code:
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/hello")
public String hello(String name) {
return "hello " + name;
}
}
pom.xml
:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.1</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>
Now I and my friend find two way to fix it.
@RequestParam("name")
to String name
before, like:
@GetMapping("/hello")
public String hello(@RequestParam("name") String name) {
return "hello " + name;
}
spring-boot-starter-parent
, like: <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/>
</parent>
I don't know if this is the Spring official expected result? Is this normal?
The parent pom contains a configuration for the maven-compiler-plugin:
<build>
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
This makes sure the code is compiles with the -parameters
flag, which makes the parameter name available at runtime.
If you don't use the parent pom, you can also configure this in your own pom by adding the plugin configuration directly:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>