I created a simple Feign Client with Spring Boot like this:
@FeignClient("spring-cloud-eureka-client")
public interface GreetingClient {
@RequestMapping("/greeting")
String greeting(@RequestParam String name);
}
But when I try just to start an application I get an error:
java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
First I didn't understand what is the reason and googled a lot but didn't find an answer. Almost excidentely I figured out that it works if to write request param name explicitly:
@RequestParam("name") String name
So my question: is it a bug or could it be configured to not write request params names explicitly?
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.