springboot整合feign時報錯
報錯信息如下(截取前段部分信息)
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportController': Unsatisfied dependency expressed through field 'reportService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportServiceImpl': Unsatisfied dependency expressed through field 'uiasUserController'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.net.topnet.base.UiasUserController': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract java.util.Map cn.net.topnet.base.UiasUserController.getMsgByUserId(java.lang.String,java.lang.String)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:847)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
at cn.net.topnet.ReportApplication.main(ReportApplication.java:24)
主要部分
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportController': Unsatisfied dependency expressed through field 'reportService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reportServiceImpl': Unsatisfied dependency expressed through field 'uiasUserController'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.net.topnet.base.UiasUserController': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract java.util.Map cn.net.topnet.base.UiasUserController.getMsgByUserId(java.lang.String,java.lang.String)
經查詢,定義的feigin接口類如下
@FeignClient(value = "uias")
public interface UiasUserController {
@GetMapping(value = "/uias/user/getMsgByUserId")
Map getMsgByUserId(@Param("userId")String userId, @Param("postId")String postId);
}
在參數定義方面使用的@Param注解,修改為@RequestParam修改后
@FeignClient(value = "uias")
public interface UiasUserController {
@GetMapping(value = "/uias/user/getMsgByUserId")
Map getMsgByUserId(@RequestParam("userId")String userId, @RequestParam("postId")String postId);
}
查詢相關博客,獲得解釋為
@RequestParam 用於controller層
(1)解決前台參數名稱與后台接收參數變量名稱不一致的問題,等價於request.getParam
(2)可設置value:指定參數名 default:指定變量初始值 require(true默認/false):指定參數是否為必傳@Param 用於dao層
個人理解為修飾參數,使得mapper.xml中的參數與后台的參數對應上,也增強了可讀性
如果兩者參數名一致得話,spring會自動進行封裝,不一致的時候就需要手動去使其對應上。
看了下注解源碼,兩種注解的接口類有所區別,限於水平有限,暫不做深入研究,如有道友指教一二,在下不勝感激涕零
CosmosRay |
||
|
|
|