Open bingoohuang opened 5 years ago
打开java8的生成参数名的开关,参考。
<parameters> boolean 3.6.2
Set to true to generate metadata for reflection on method parameters.
Default value is: false.
User property is: maven.compiler.parameters.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
跟踪~/.m2/repository/org/springframework/boot/spring-boot-starter-parent/2.1.1.RELEASE/spring-boot-starter-parent-2.1.1.RELEASE.pom
,发现其中maven-compiler-plugin的配置已经添加了parameters参数了。
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
再继续跟踪~/.m2/repository/org/springframework/boot/spring-boot-dependencies/2.1.1.RELEASE/spring-boot-dependencies-2.1.1.RELEASE.pom
,发现compiler版本已经到了3.8.0了。
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
在SpringMVC中,强烈建议还是给@RequestParam
加上属性名称,例如@RequestParam("name")
,将请求属性名称与方法参数名称进行分离。
如果不加,例如下面这样,在代码重构时,若改变name参数的名称,也会导致请求参数的名称发生变化了,例如把name改成了x,那么就不只是方法参数名称变化了而已,也会导致客户端HTTP请求参数名称也需要从name变化成x了。
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "messages/hello" + name;
}
pom.xml
运行
mvn clean package
命令,然后javap -v target/classes/HelloController.class
可以看到,hello函数上有一个本地变量表LocalVariableTable在pom.xml中添加bytebuddy插件后,再次运行
mvn clean package
和javah
命令后,看到LocalVariableTable没有了,导致hello方法无法正确调用。然后访问
curl "http://127.0.0.1:8080?addr=bjca&name=bingoo"
会打印出