没有名称的Spring MVC RequestParam在本机映像上不起作用

nr7wwzry  于 8个月前  发布在  Spring
关注(0)|答案(1)|浏览(77)

Spring Boot 3.1.3
graalvm-jdk-17.0.8+9.1
native-maven-plugin 0.9.5
RequestParam中没有名称的控制器方法不起作用(下面的异常):

@GetMapping(value="/test", produces=MediaType.TEXT_PLAIN_VALUE)
public String test(@RequestParam(required=false) String category)

与名称相同的注解工作正常:

@GetMapping(value="/test", produces=MediaType.TEXT_PLAIN_VALUE)
public String test(@RequestParam(name = "category", required=false) String category)

例外情况:

java.lang.IllegalArgumentException: Name for argument of type
[java.lang.String] not specified, and parameter name information notfound in class file either.
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.updateNamedValueInfo(AbstractNamedValueMethodArgumentResolver.java:177)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.getNamedValueInfo(AbstractNamedValueMethodArgumentResolver.java:154)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:99)

无法使用spring Boot 作为父pom,因此导入它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>3.1.3</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

用于生成本机映像的pom片段,遵循wiki(提示):

<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <configuration>
        <fallback>false</fallback>
        <verbose>true</verbose>
        <quickBuild>true</quickBuild>
        <metadataRepository>
            <enabled>true</enabled>
        </metadataRepository>       
        <jvmArgs>
            <jvmArg>-XX:+UseContainerSupport</jvmArg>
            <jvmArg>-XX:MaxRAMPercentage=70</jvmArg>                                
        </jvmArgs>                          
        <buildArgs>
            <arg>-H:+ReportExceptionStackTraces</arg>
            <!-- WA for https://github.com/oracle/graal/issues/4757 -->
            <arg>-H:-UseContainerSupport</arg>
            <!-- Custom reachability metadata for 3rd parties, not yet supported in: https://github.com/oracle/graalvm-reachability-metadata/tree/master/metadata --> 
            <arg>-H:ReflectionConfigurationResources=META-INF/native-image/reflect-config.json</arg>
        </buildArgs>                            
        <requiredVersion>22.3</requiredVersion>
    </configuration>
    <executions>
        <execution>
            <id>add-reachability-metadata</id>
            <goals>
                <goal>add-reachability-metadata</goal>
            </goals>
        </execution>
    </executions>
</plugin>

当试图添加相同的方法没有名称的Demo应用程序它的工作完美,所以我明白了一些是错误的,我的项目,但无法找到根本原因。

@GetMapping(value="/test", produces=MediaType.TEXT_PLAIN_VALUE)
public String test(@RequestParam(required=false) String category){
    System.out.println("category: " + category);
    return category;
}
nwwlzxa7

nwwlzxa71#

感谢M.代努姆的评论,在检查Sping Boot parent pom后找到了根本原因。需要在maven-compiler-plugin中添加parameters=true以在编译的类中保留方法参数名称。然而,最好修复代码并将参数名称添加到spring注解中,以通过在编译的类中保留这些参数名称来不增加最终的本机映像大小。

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

相关问题