背景:在做多服務之間需要使用FeignClient進行服務調用的時候,出現PathVariable annotation was empty on param 0.,根據提示需要指定value的值,以下爲具體解決過程
/**
* @Package: com.aimsphm.nuclear.data.feign
* @Description: <服務調用>
* @Author: MILLA
* @CreateDate: 2020/3/31 15:22
* @UpdateUser: MILLA
* @UpdateDate: 2020/3/31 15:22
* @UpdateRemark: <>
* @Version: 1.0
*/
@Component
@FeignClient(value = "nuclear-core", fallback = CoreHystrixClientFallback.class)
public interface CoreServiceFeignClient {
@GetMapping("{deviceId}/statistics/warning")
ReturnResponse statisticsWarmingPoints(@PathVariable Long deviceId, Long startTime, Long endTime);
}
啓動後報錯代碼爲:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportApplication': Unsatisfied dependency expressed through field 'reportUtils'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportUtils': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportDataOperationServiceImpl': Unsatisfied dependency expressed through field 'coreFeignClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aimsphm.nuclear.report.feign.CoreServiceFeignClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: PathVariable annotation was empty on param 0. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE]
第二行實際爲重點語句:描述爲PathVariable註解作爲第一個參數不能爲空
改動爲:
@GetMapping("{deviceId}/statistics/warning") ReturnResponse statisticsWarmingPoints(@PathVariable(value = "deviceId") Long deviceId, Long startTime, Long endTime)
重新啓動後:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportApplication': Unsatisfied dependency expressed through field 'reportUtils'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportUtils': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportDataOperationServiceImpl': Unsatisfied dependency expressed through field 'coreFeignClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aimsphm.nuclear.report.feign.CoreServiceFeignClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.aimsphm.nuclear.common.response.ReturnResponse com.aimsphm.nuclear.report.feign.CoreServiceFeignClient.statisticsWarmingPoints(java.lang.Long,java.lang.Long,java.lang.Long) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:847) ~[spring-beans-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.13.RELEASE.jar:5.1.13.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.12.RELEASE.jar:2.1.12.RELEASE] at com.aimsphm.nuclear.report.ReportApplication.main(ReportApplication.java:28) [classes/:na]
依然是第二行描述爲:在body中的參數有太多了,所以此時,首先確定參數是否是在body中,如果是的話,應該合成一個參數進行傳遞,如果不是的話,看是否應該是採用@RequestParam註解,本例中不是body中的參數,故採用@RequestParam註解
再次改動後:
@GetMapping("{deviceId}/statistics/warning") ReturnResponse statisticsWarmingPoints(@PathVariable(value = "deviceId") Long deviceId, @RequestParam Long startTime, @RequestParam Long endTime);
再次啓動後仍舊報錯:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportApplication': Unsatisfied dependency expressed through field 'reportUtils'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportUtils': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportDataOperationServiceImpl': Unsatisfied dependency expressed through field 'coreFeignClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.aimsphm.nuclear.report.feign.CoreServiceFeignClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: RequestParam.value() was empty on parameter 1
可以看到,現在報錯是說參數2的value註解是空,以此再次修改
@GetMapping("{deviceId}/statistics/warning") ReturnResponse statisticsWarmingPoints(@PathVariable(value = "deviceId") Long deviceId, @RequestParam(value = "startTime") Long startTime, @RequestParam(value = "endTime") Long endTime);
然後重新啓動,成功!!
僅此,記錄,下次少走彎路...