一、场景
1、创建Feign客户端报错
二、报错信息
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxUtil': Unsatisfied dependency expressed through field 'xxxApiFeignClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xxx.feign.xxxApiFeignClient': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.alibaba.fastjson.JSONObject com.xxx.feign.xxxApiFeignClient.getHotQuestions(java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,java.lang.String)
Warnings:
-
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657)
... 20 common frames omitted
三、Feign客户端代码
@FeignClient(name = "defaultxxxApi-cloud", contextId = "xxxApiFeignClient", path = "defaultxxxApiPath")
@RefreshScope
public interface xxxApiFeignClient {
@PostMapping(value = "/xxx_api/getHotQuestions")
JSONObject getHotQuestions(String cId, String appId,
String apiId, String openId,
int top, String type);
}
四、原因
当Feign客户端接口参数数量超过1个时,需要为接口参数指定参数名称
五、解决
使用@RequestParam
注解为接口参数指定参数名称
@FeignClient(name = "defaultxxxApi-cloud", contextId = "xxxApiFeignClient", path = "defaultxxxApiPath")
@RefreshScope
public interface xxxApiFeignClient {
@PostMapping(value = "/robot_api/visitor/getHotQuestions")
JSONObject getHotQuestions(@RequestParam(value = "cId") String cId, @RequestParam(value = "appId") String appId,
@RequestParam(value = "apiId") String apiId, @RequestParam(value = "openId") String openId,
@RequestParam(value = "top") int top, @RequestParam(value = "type") String type);
}