RequestParam.value() was empty on parameter 0

本文探讨了SpringMVC与Spring Cloud Feign在参数名发现上的不同表现,详细分析了编译选项-parameters对参数名信息保存的影响,以及这如何导致Feign在接口方法中无法获取参数名的问题。

Both Spring MVC and Spring cloud feign are using the same ParameterNameDiscoverer - named DefaultParameterNameDiscoverer to find parameter name. It tries to find the parameter names with the following step.

First, it uses StandardReflectionParameterNameDiscoverer. It tries to find the variable name with reflection. It is only possible when your classes are compiled with -parameters.

Second, if it fails, it uses LocalVariableTableParameterNameDiscoverer. It tries to find the variable name from the debugging info in the class file with ASM libraries.

The difference between Spring MVC and Feign occurs here. Feign uses above annotations (like @RequestParam) on methods of Java interfaces. But, we use these on methods of Java classes when using Spring MVC. Unfortunately, javac compiler omits the debug information of parameter name from class file for java interfaces. That's why feign fails to find parameter name without -parameter.

Namely, if you compile your code with -parameters, both Spring MVC and Feign will succeed to acquire parameter names. But if you compile without -parameters, only Spring MVC will succeed.

As a result, it's not a bug. it's a limitation of Feign at this moment as I think.

 

来自 Stack Overflow   主要原因是 编译后字节码文件参数信息的变化,后文给出结论

 

接口类源文件

public interface InterfaceParams {

String withNothing(String a, Integer b);

 

String withValue(@RequestParam(value = "a") String a, @RequestParam(value = "b") Integer b);

 

String withAnnotation(@RequestParam String a, @RequestParam Integer b);

}

 

不是用-parameters

javac -cp /Users/tz/.m2/repository/com/google/guava/guava/23.0/guava-23.0.jar:/Users/tz/.m2/repository/org/springframework/spring-web/5.0.10.RELEASE/spring-web-5.0.10.RELEASE.jar /Users/tz/work/**/feign/InterfaceParams.java

反编译结果1

 

public interface InterfaceParams {

java.lang.String withNothing(java.lang.String s, java.lang.Integer integer);

 

java.lang.String withValue(@org.springframework.web.bind.annotation.RequestParam("a") java.lang.String s, @org.springframework.web.bind.annotation.RequestParam("b") java.lang.Integer integer);

 

java.lang.String withAnnotation(@org.springframework.web.bind.annotation.RequestParam java.lang.String s, @org.springframework.web.bind.annotation.RequestParam java.lang.Integer integer);

}

使用-parameters

javac -parameters -cp /Users/tz/.m2/repository/com/google/guava/guava/23.0/guava-23.0.jar:/Users/tz/.m2/repository/org/springframework/spring-web/5.0.10.RELEASE/spring-web-5.0.10.RELEASE.jar /Users/tz/work/**/feign/InterfaceParams.java

 

反编译结果2

public interface InterfaceParams {

java.lang.String withNothing(java.lang.String a, java.lang.Integer b);

 

java.lang.String withValue(@org.springframework.web.bind.annotation.RequestParam("a") java.lang.String a, @org.springframework.web.bind.annotation.RequestParam("b") java.lang.Integer b);

 

java.lang.String withAnnotation(@org.springframework.web.bind.annotation.RequestParam java.lang.String a, @org.springframework.web.bind.annotation.RequestParam java.lang.Integer b);

}

3 实体类

public class InterfaceImplParams {

String withNothing(String a, Integer b){

return a;

}

 

String withValue(@RequestParam(value = "a") String a, @RequestParam(value = "b") Integer b){

return a;

}

 

String withAnnotation(@RequestParam String a, @RequestParam Integer b){

return a;

}

}

 

javac -parameters -cp /Users/tz/.m2/repository/com/google/guava/guava/23.0/guava-23.0.jar:/Users/tz/.m2/repository/org/springframework/spring-web/5.0.10.RELEASE/spring-web-5.0.10.RELEASE.jar /Users/tz/work/**/business/feign/InterfaceImplParams.java

 

反编译结果

public class InterfaceImplParams {

public InterfaceImplParams() { /* compiled code */ }

 

java.lang.String withNothing(java.lang.String a, java.lang.Integer b) { /* compiled code */ }

 

java.lang.String withValue(@org.springframework.web.bind.annotation.RequestParam("a") java.lang.String a, @org.springframework.web.bind.annotation.RequestParam("b") java.lang.Integer b) { /* compiled code */ }

 

java.lang.String withAnnotation(@org.springframework.web.bind.annotation.RequestParam java.lang.String a, @org.springframework.web.bind.annotation.RequestParam java.lang.Integer b) { /* compiled code */ }

}

 

结论: 指定 value  如RequestParam("b") ,即可。

继续深入ASM,可以参考之前的文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自驱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值