一、前言
在用分布式架構(gòu)SpringBoot的SpringCloud技術(shù)開發(fā)過程中,F(xiàn)eignClient 是一個(gè)常用的注解,且很重要的功能。
簡單理解就是,分布式架構(gòu)服務(wù)之間,各子模塊系統(tǒng)內(nèi)部通信的核心。
一般在一個(gè)系統(tǒng)調(diào)用另一個(gè)系統(tǒng)的接口時(shí)使用,如下:
注解
@FeignClient("XXX")
public interface XX{
? ....
}
該注解一般創(chuàng)建在 interface 接口中,然后在業(yè)務(wù)類@Autowired進(jìn)去使用非常簡單方便。
二、問題背景
創(chuàng)建好interface接口后,當(dāng)然要把調(diào)用該服務(wù)的接口方法定義出來,該方法對應(yīng)本FeignClient的controller接口,必須重寫該接口方法(返回對象,參數(shù)值完全一樣)。
啟動(dòng)項(xiàng)目出現(xiàn)如下報(bào)錯(cuò)時(shí),咋一看以為是在業(yè)務(wù)類中調(diào)用該接口方法時(shí),傳參為空null而報(bào)錯(cuò)。
FactoryBean threw exception on object creation; nested exception is
java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
當(dāng)把傳參用數(shù)據(jù)代替時(shí),重新啟動(dòng)時(shí);任然報(bào)如上錯(cuò)誤。
貼一個(gè)報(bào)錯(cuò)全截圖
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") 接口類中,檢查每個(gè)方法的參數(shù)定義時(shí):
是否有如下情形
@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
這個(gè)參數(shù)少了個(gè)value = "XXCode", 這個(gè)是Spring 4.0版本后,@RequestParam 注解對參數(shù)傳值有了很好的封裝特性并嚴(yán)格校驗(yàn)。
改為:@RequestParam(value = "XXCode", required = false) String XXCode
之后,問題完美解決;重啟項(xiàng)目正常。
另外,插一句:當(dāng)在項(xiàng)目多個(gè)地方調(diào)用同一個(gè)@FeignClien("XX")某項(xiàng)目時(shí),在多個(gè)包中創(chuàng)建接口,并無影響。
---------------------文章來自CSDN技術(shù)博客