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,可以参考之前的文章

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

被折叠的 条评论
为什么被折叠?



