Springboot FeignClient调用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Springboot FeignClient调用

涟漪海洋   2022-05-23 我要评论

背景:在做多服务之间需要使用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);

然后重新启动,成功!!

仅此,记录,下次少走弯路...

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们