There is already ‘xxx‘ bean method 记一次hystrix使用报错

矫情吗;* 2022-10-30 01:26 137阅读 0赞

年假快过完了, 不知道吃吃喝喝的你, 是否还在学习呢, 最近hystrix在整合feign时候, 报了这样一个错误

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.mashibing.eurekaserver.ConsumerAPI' method
  2. com.mashibing.eurekaserver.ConsumerAPI#getMap(Integer)
  3. to { GET /User/getMap}: There is already 'userProviderBack' bean method
  4. com.mashibing.eurekaserver.UserProviderBack#getMap(Integer) mapped.
  5. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  8. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  9. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  10. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  12. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  13. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  14. at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  15. at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java:40002) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  16. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:41008) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
  17. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  18. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  19. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  20. at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  21. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  22. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  23. at com.mashibing.eurekaserver.UserConsumerApplication.main(UserConsumerApplication.java:17) [classes/:na]
  24. Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.mashibing.eurekaserver.ConsumerAPI' method
  25. com.mashibing.eurekaserver.ConsumerAPI#getMap(Integer)
  26. to { GET /User/getMap}: There is already 'userProviderBack' bean method
  27. com.mashibing.eurekaserver.UserProviderBack#getMap(Integer) mapped.

当时思来想去, 不知道哪里错误了, 我就想到到了以下爱几个点

  1. 我的配置文件是不是没有开启hystrix?
  2. 是不是有一个重复的mapping?
    算了直接贴出来点干货吧

    这是我的properties

    feign.hystrix.enabled=true

    这是feign

    @FeignClient(name = “user-provider”,fallback = AliveBack.class)
    public interface ConsumerApi {

    1. @RequestMapping(value = "/User/alive",method = RequestMethod.GET)
    2. public String alive();
    3. @RequestMapping(value = "/User/getById",method = RequestMethod.GET)
    4. public String getById(Integer id);

    }

    这是具体实现

    import java.util.Map;

    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.RequestMapping;
    @Component
    public class AliveBack implements ConsumerApi{

    1. @Override
    2. public String alive() {
    3. // TODO Auto-generated method stub
    4. return "aaa";
    5. }
    6. @Override
    7. public String getById(Integer id) {
    8. // TODO Auto-generated method stub
    9. return null;
    10. }

    }

乍一看, 没啥毛病啊,mapping也找不到多余的, 但是找到的我的api接口时候,
@RequestMapping(“User”)这个注解注释掉就行了, 如果你在feign上加上hystrix时候会重复注册这个User里的alive,getById等方法.没办法只能注释掉.

  1. @RequestMapping("User")
  2. public interface UserApi {
  3. /** * 测试 * @return lalala */
  4. @GetMapping("/alive")
  5. String isAlive();
  6. @GetMapping("/getById")
  7. Integer getById(@RequestParam("id") Integer id);
  8. }

再次访问OK了!
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读