运行springcloud项目出现如下报错:
FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
这个错误产生的缘由是我使用了 Feign,在配置了 @FeignClient 注解的接口上声明的方法中的参数使用了 @RequestParam 注解,可是 @RequestParam 注解并无指定 value 属性。具体以下:
/**
* @FeignClient:name 远程服务名,即:spring.application.name配置的名称
* 此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
*/
@Component
@FeignClient(name= "spring-cloud-producer")
public interface HelloController {
/**
* 测试feign远程方法调用,这个方法必须与服务提供者controller的方法参数保持一致
* @param name
* @return
*/
@RequestMapping("hello")
public String hello(@RequestParam String name);
}
这个时候,咱们在启动项目,就会出现“nested exception is java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0”异常。
进而会影响到 XttblogService 实例的建立,雪崩效应,致使整个项目没法启动。
正确的:
在 SpringMVC 和 Springboot 中均可以使用 @RequestParam 注解,不指定 value 的用法,为何到了 Spring cloud 中的 Feign 这里就不行了呢?负载均衡
这是由于和 Feign 的实现有关。Feign 的底层使用的是 httpclient,在低版本中会产生这个问题,高版本中已经对这个问题修复了。