feign的callback设定后,项目启动错误

错误如下:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-06-16 22:20:47.295 [main] ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.newtouch.springcloud.iclient.IUser' method 
public abstract java.util.Map<java.lang.String, java.lang.Object> com.newtouch.springcloud.iclient.IUser.addUser()
to {[/user/addUser],methods=[POST]}: There is already 'userFollBack' bean method
public java.util.Map<java.lang.String, java.lang.Object> com.newtouch.springcloud.iclient.UserFollBack.addUser() mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.newtouch.springcloud.ShopApplication.main(ShopApplication.java:15)
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.newtouch.springcloud.iclient.IUser' method 
public abstract java.util.Map<java.lang.String, java.lang.Object> com.newtouch.springcloud.iclient.IUser.addUser()
to {[/user/addUser],methods=[POST]}: There is already 'userFollBack' bean method
public java.util.Map<java.lang.String, java.lang.Object> com.newtouch.springcloud.iclient.UserFollBack.addUser() mapped.
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:540)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:127)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 16 common frames omitted
@FeignClient(name="SERVICE-USER",fallback=UserFollBack.class)
@RequestMapping("/user")
public interface IUser {

    @RequestMapping(method=RequestMethod.POST,value="/addUser")
    public Map<String, Object> addUser();
    
}
@Component
public class UserFollBack implements IUser {
    
    
    public Map<String, Object> addUser(){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("ok","ss");
        return resultMap;
    }
}

当服务提供者不可用时,无法通过提供的接口生成service bean,此时就要使用fallback中提供的bean进行服务。
原先报错是因为HelloService中的端口被绑定到了对应的方法上,也就是/refactor/hello4被占用,此时再生成RefactorHelloServiceFallback 的bean时,就会出现端口已经被占用的错误(先后顺序可能不同,都是这个问题),我把RefactorHelloServiceFallback 中的端口变更(添加@RequestMapping("/fallback"),或者其他变更端口的方法)后就没有问题了。
重构前不会出错,是因为既没有公用的HelloService,也没有fallback用到的RefactorHelloServiceFallback ,不存在端口重复的问题。

更改之后启动成功:

@Component
public class UserFollBack implements IUser {
    
    @RequestMapping("/fallback")
    public Map<String, Object> addUser(){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("ok","ss");
        return resultMap;
    }
}

 requestmappering不要加在类上也是可以的 

@FeignClient(name="SERVICE-USER",fallback=UserFollBack.class)
public interface IUser {

    @RequestMapping(method=RequestMethod.POST,value="user/addUser")
    public Map<String, Object> addUser();
    
}

@Component
class UserFollBack implements IUser {

    public Map<String, Object> addUser(){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("ok","ss");
        return resultMap;
    }
}

 

posted @ 2018-06-16 22:27  随风而逝,只是飘零  阅读(1935)  评论(0编辑  收藏  举报