Spring Cloud Feign nested exception is java.lang.IllegalStateException

Spring Cloud Feign 使用时抛出异常 nested exception is java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0

Feign是一个声明式Web Service客户端。
使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也>支持JAX-RS标准的注解。
Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。
Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

出现这个问题也是很奇葩,这里记录一下
这个错误产生的原因是我使用了 Feign,在配置了 @FeignClient 注解的接口上声明的方法中的参数使用了 @RequestParam 注解,但是 @RequestParam 注解并没有指定 value 属性。具体如下:
错误的:

	package eureka.utils;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: yaoZhenGuo
 * @date: 2019/01/17
 * @FeignClient:name 远程服务名,即:spring.application.name配置的名称
 * 此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
 */

@Component
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {

    /**
     * 测试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 实例的创建,雪崩效应,导致整个项目无法启动。

正确的:

@Component
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {

    /**
     * 测试feign远程方法调用,这个方法必须与服务提供者controller的方法参数保持一致
     * @param name
     * @return
     */
    @RequestMapping("hello")
    public String hello(@RequestParam(value = "name") String name);
}

在 SpringMVC 和 Springboot 中都可以使用 @RequestParam 注解,不指定 value 的用法,为什么到了 Spring cloud 中的 Feign 这里就不行了呢?

这是因为和 Feign 的实现有关。Feign 的底层使用的是 httpclient,在低版本中会产生这个问题,高版本中已经对这个问题修复了。

参考:https://www.xttblog.com/?p=3006

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值