Feign调用Hystrix报错There is already ‘computeClientHystrix’ bean method mapped

使用Feign调用Hystrix进行断路器的学习,一直报错There is already ‘computeClientHystrix’ bean method mapped 
相关类如下: 
调用远程服务的接口类

@FeignClient(value="COMPUTE-SERVICE", fallback=ComputeClientHystrix.class)
@RequestMapping("computer")
public interface ComputerController {
    @RequestMapping(method = RequestMethod.GET, value = "/add")
    Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}

实现接口类的实现类:

@Component
public class ComputeClientHystrix implements ComputerController{
    @Override
    public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
        return -9999;
    }
}

看了下日志,先map了实现类的add方法,然后又去map了接口中的add方法,因为两者的继承关系,所以add方法的url是相同的,所以spring认为两个方法重复了,就报错了

[           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/computer/add],methods=[GET]}" onto public java.lang.Integer com.zeng.learn.spring.cloud.consumer.server.ComputeClientHystrix.add(java.lang.Integer,java.lang.Integer)
2017-04-23 12:04:31.744  WARN 10320 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.zeng.learn.spring.cloud.consumer.ServerController.ComputerController' method 
public abstract java.lang.Integer com.zeng.learn.spring.cloud.consumer.ServerController.ComputerController.add(java.lang.Integer,java.lang.Integer)
to {[/computer/add],methods=[GET]}: There is already 'computeClientHystrix' bean method
public java.lang.Integer com.zeng.learn.spring.cloud.consumer.server.ComputeClientHystrix.add(java.lang.Integer,java.lang.Integer) mapped.

改成如下的形式就ok了,应该是把url映射放在接口中。 
接口:

@FeignClient(value="COMPUTE-SERVICE", fallback=ComputeClientHystrix.class)
public interface ComputerController {
    @RequestMapping(method = RequestMethod.GET, value = "/computer/add")
    Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}

实现:

@Component
public class ComputeClientHystrix implements ComputerController{
    @Override
    public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
        return -9999;
    }
}

可以注意到RequestMapping中的value是“/conputer/add”,一般我们在spring mvc中,前面一致的都会放在class上,但是如果把@RequestMapping(value = “/computer”)放在接口上的话,还是会报错。

进入FeignClient注解里看了下属性,有一个path属性,是用来表示接口内所有方法请求的前缀的,而不是像springmvc,在接口上配置前缀@RequestMapping

@FeignClient(value = "spring-cloud-producer-chidao",fallback = StudentRemoteHystrix.class,path = "/student")
//@RequestMapping(value = "/student")
public interface StudentRemoteService {
    @GetMapping(value = "/selectAll")
    public Result<List<Student>> selectAll();
    @GetMapping(value = "/findByName")
    public Result<Student> findByName(@RequestParam(value = "name", required = true) String name);

这样就可以了

转载于:https://my.oschina.net/u/2000675/blog/2244769

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值