Springboot FeignClient调用Method has too many Body parameters解决

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

然后重新启动,成功!!

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

到此这篇关于Springboot FeignClient微服务间调用Method has too many Body parameters 解决的文章就介绍到这了,更多相关Springboot FeignClient微服务间调用 内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springboot FeignClient注解及参数

    一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 @FeignClient(name = "github-client", url = "https://api.github.com", configuration = GitHubExampleConfig.class) public interface GitHubClient { @Request

  • Springboot FeignClient调用Method has too many Body parameters解决

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

  • springboot mybatis调用多个数据源引发的错误问题

    目录 springboot mybatis调用多个数据源错误 报错 解决方法 springboot-mybatis多数据源及踩坑 springboot项目结构如下 springboot配置文件内容如下 动态数据源的配置类如下 最关键的来了 springboot mybatis调用多个数据源错误 报错 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init meth

  • 解决feignClient调用时获取返回对象类型匹配的问题

    feignClient调用时获取返回对象类型匹配 feignClient是springCloud体系中重要的一个组件,用于微服务之间的相互调用,底层为httpClient,在之前的应用中,我一直以为A服务提供的方法返回类型为对象的话,那么调用A服务的B服务必须也用字段类型以及命名完全相同的对象来接收,为此我验证了一下,发现不是必须用完全相同的对象来接收,比如,可以用map<String,Object>或者Object来接收,然后解析. 当然,复杂对象我还是推荐用一个完全相同的对象来接收. 下面

  • 使用FeignClient调用POST表单Body内没有参数问题

    目录 FeignClient调用POST表单Body没有参数 FeignClient参数问题 使用Get方式调用服务提供者 使用Post方式调用服务提供者 三种情况(无参.单参.多参) @RequestBody和@RequestParam的区别 FeignClient调用POST表单Body没有参数 1.问题:在使用FeignClient调用第三方接口,对方是普通POST接口,但是调用返回400,后发现请求体没有携带需要的参数. 2.查找资料发现,FeignClient默认POST请求发送参数为

  • SpringBoot this调用@Bean效果详解

    目录 思考的起源 查找信息 更远一步 在一个@Bean方法内,this调用同一个类的@Bean方法会有什么效果呢? 思考的起源 首先上代码: public class BeanOne { } public class BeanTwo { public BeanTwo(BeanOne beanOne){ } } @Configuration public class BeanConfigTest { @Bean @ConditionalOnMissingBean public BeanOne be

  • 基于FeignClient调用超时的处理方案

    FeignClient调用超时 出现问题的前提 SpringCloud间FeignClient调用出现ReadTimeOut的情况 FeignClient服务间调用的默认超时时间为2秒 网上查找的解决方案 关闭Hystrix(馊主意) 延长超时间(Spring众多超时时间配置繁多) 解决方案 为FeignClient添加自定义的超时配置,结合Nacos可控 话不多说,贴出源码 /** * FeignClient配置类 * * @author gralves * @date 2020/8/10 *

  • springboot动态调用实现类方式

    目录 springboot动态调用实现类 springboot手动获取实现类 springboot动态调用实现类 定义规则的多种类型 /** * 数据规则处理类型枚举 */ public enum RuleType { MYRULEBYID1, MYRULEBYID2 } 接口 import java.util.List; import java.util.Map; public interface DataRuleParse { /** * 获取规则处理的类型 * @return */ Rul

  • 使用FeignClient调用远程服务时整合本地的实现方法

    目录 FeignClient调用远程服务时整合本地 尝试将本地方法加入Feign接口 尝试通过实现两个接口 IS->HAS FeignClient服务之间调用 服务A需调用服务B的test方法 A的application配置加 FeignClient调用远程服务时整合本地 包装一个用户服务,一部分功能需要调用远程服务,而另一部分功能调用本地方法,如: @FeignClient(value="USER-SERVICE") public interface RemoteUserSer

  • springboot html调用js无效400问题及解决

    目录 springboot html调用js无效400 例如 无法访问css,js,图片等静态资源的三种方案 第一种解决方法 第二种解决方法 第三种解决方法 springboot html调用js无效400 html板在templates下面,js文件在static下面,在模板中引用时不需要加static这个路径. 例如 src   └─main      ├─java      └─resources          ├─static          │  ├─css          │

随机推荐