前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >FeignClient注解及参数问题---SpringCloud微服务

FeignClient注解及参数问题---SpringCloud微服务

作者头像
程序大视界
发布2020-07-21 16:07:53
1.5K0
发布2020-07-21 16:07:53
举报
文章被收录于专栏:程序大视界程序大视界

一、前言

在用分布式架构SpringBoot的SpringCloud技术开发过程中,FeignClient 是一个常用的注解,且很重要的功能。

简单理解就是,分布式架构服务之间,各子模块系统内部通信的核心。

一般在一个系统调用另一个系统的接口时使用,如下:

注解

@FeignClient("XXX")
public interface XX{
   ....
}

该注解一般创建在 interface 接口中,然后在业务类@Autowired进去使用非常简单方便。

二、问题背景

创建好interface接口后,当然要把调用该服务的接口方法定义出来,该方法对应本FeignClient的controller接口,必须重写该接口方法(返回对象,参数值完全一样)。

启动项目出现如下报错时,咋一看以为是在业务类中调用该接口方法时,传参为空null而报错。

FactoryBean threw exception on object creation; nested exception is
 java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0

当把传参用数据代替时,重新启动时;任然报如上错误。

贴一个报错全截图

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'withdrawCountRecordController': Unsatisfied dependency expressed through field 'withdrawCountService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'withdrawCountServiceImpl': Unsatisfied dependency expressed through field 'cumClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.epaylinks.efps.pas.clr.client.CumClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

三、解决办法

在@FeignClien("XX") 接口类中,检查每个方法的参数定义时:

是否有如下情形

@RequestMapping(value="/XXX/query", method = RequestMethod.GET)
    public PageResult<XXutionResp> query(@RequestParam(required = false) String XXCode,
                                             @RequestParam(value = "XXnName",required = false) String institutionName,
                                             @RequestParam(value = "startTime",required = false) String startTime,

问题就在这里:

@RequestParam(required = false) String XXCode

这个参数少了个value = "XXCode", 这个是Spring 4.0版本后,@RequestParam 注解对参数传值有了很好的封装特性并严格校验。

改为:

@RequestParam(value = "XXCode", required = false) String XXCode

之后,问题完美解决;重启项目正常。

另外,插一句:当在项目多个地方调用同一个@FeignClien("XX")某项目时,在多个包中创建接口,并无影响。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序大视界 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档