org.springframework.beans.factory.UnsatisfiedDependencyException
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 |
||
cosmosray@aliyun.com |