一、前言
在用分布式架构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")某项目时,在多个包中创建接口,并无影响。
---------------------文章来自CSDN技术博客