springMvc:Request processing failed: java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflectio......

异常描述:

Request processing failed: java.lang.IllegalArgumentException: 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.

请求处理失败:java.lang.IllegalArgumentException:未指定 [java.lang.String] 类型的参数的名称,并且无法通过反射获得参数名称信息。确保编译器使用“-parameters”标志。

异常截图:

 代码块

//@RestController
@Controller
@RequestMapping("/param" )
public class ParamController {
    //直接接收/param/data?name=root&age=18
    @RequestMapping("/data")
    @ResponseBody
    public String data(String name) {
        int age=18;
        System.out.println("name" + name + ",age" + age);
        return "name:" + name + ",age:" + age;
    }
    @GetMapping("/{id}")
    public String getId(@PathVariable String id) {
        return "id: " + id;
    }

}

解决方案

 add "-parameters" to compile option.

 

 

问题原因:

Compile Option "-parameters" 

I found this because Compile Option "-parameters" is disabled.

因为编译选项“-参数”被禁用。

-parameters

Generates metadata for reflection on method parameters. Stores formal parameter names of constructors and methods in the generated class file so that the method java.lang.reflect.Executable.getParameters from the Reflection API can retrieve them.

为方法参数的反射生成元数据。在生成的类文件中存储构造函数和方法的正式参数名,以便来自反射API的方法java.lang.reflect.Executable.getParameters可以检索它们

 

 

方法二:

添加 pom插件设置

<parameters>true</parameters>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <parameters>true</parameters>
                </configuration>
            </plugin>
        </plugins>
    </build>

tips:可能需要maven操作clean,compile来重建

 问题原因:

这可确保使用 -parameters 标志编译代码,从而使参数名称在运行时可用。

如果不使用父 pom,也可以在自己的pom中直接添加插件配置来配置:

 

参考:

https://youtrack.jetbrains.com/issue/IDEA-339211/Parameter-Name-Reflection-Fails-in-IntelliJ-Builds-with-Spring-Boot-3

https://oldmoon.top/post/191#maven-compiler-plugin

posted @ 2024-05-23 00:56  困到很想醒  阅读(1380)  评论(0编辑  收藏  举报