Spring如何仅通过形式参数名匹配查询参数?

gab*_*197 8 java bytecode bytecode-manipulation spring-restcontroller

假设我有以下代码片段:

@RequestMapping(method = RequestMethod.GET)
public List<Article> getArticles(@RequestParam int offset,
                                 @RequestParam int limit) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

当参数名称未明确声明为注释参数时,Spring如何将HTTP查询参数与正确的形式参数匹配?

它是否假设形式参数名称始终存在于字节码中?

据我了解,并非总是如此.只有在以下情况下才能从字节码中检索形式参数名称:

a)类文件已使用-parameters javac选项编译

b)类文件已使用-g(或-g:vars)javac选项进行编译,该选项添加包含真实变量名称的调试信息(因为形式参数存储为方法的第一个局部变量)

Ole*_*hov 6

为了找到问题的答案,我maven使用以下命令构建了Spring REST应用程序:

mvn clean install -X
Run Code Online (Sandbox Code Playgroud)

build.log包含以下调试信息:

[DEBUG] Command line options:

... -g -nowarn -target 1.8 -source 1.8 -encoding UTF-8
Run Code Online (Sandbox Code Playgroud)

我们可以看到,该   -g  选项默认存在,它生成调试信息,包括局部变量.

为了证明不是这样,我maven-compiler-plugin使用以下选项更新了配置:

-g:none
Run Code Online (Sandbox Code Playgroud)

配置示例:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgument>-g:none</compilerArgument>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

之后,我的Web服务开始抛出错误:

java.lang.IllegalArgumentException: Name for argument type [java.lang.String] not
available, and parameter name information not found in class file either.
Run Code Online (Sandbox Code Playgroud)

似乎默认maven-compiler-plugin包含-g选项.这允许Spring在运行时检索参数名称.