OpenFeign使用Hystrix报错:Ambiguous mapping. Cannot map xx method...

今天做项目的时候,feign调用的时候写了一个Hystrix的服务降级处理类,但是运行时报了以下错误:

Ambiguous mapping. Cannot map 'com.guli.vod.feign.VodFeign' method 
com.guli.vod.feign.VodFeign#removeVideo(String)
to {DELETE /vod/video/{id}}: There is already 'vodFallback' bean method
com.guli.vod.fallback.VodFallback#removeVideo(String) mapped.

原来写的FeignClient:(这里吐槽一下,Restful api根本没办法设计批量删除的路径嘛,DELETE方法又不推荐使用请求体,而且将资源id放在请求体里也不符合Restful url的语义)

@Component
@FeignClient(value = "service-vod",fallback = VodFallback.class)
@RequestMapping("/vod")
public interface VodFeign {
    @DeleteMapping(value = "/video/{id}")
    public Result removeVideo(@PathVariable("id") String videoId);

    @DeleteMapping(value = "/video/delete-batch")
    public Result removeVideoList(@RequestParam("videoIdList") List<String> videoIdList);
}

服务降级处理类:

@Component
public class VodFallback implements VodFeign {

    @Override
    public Result removeVideo(String videoId) {
        return Result.error().msg("time out");
    }

    @Override
    public Result removeVideoList(List<String> videoIdList) {
        return Result.error().msg("time out");
    }
}

原因在于VodFallback实现了VodFeign接口后,把@RequestMapping的路径映射也继承了过去,而两个类都会注册bean实例对象,就会出现映射冲突的问题,最后我是将FeignClient的@RequestMapping都写在了方法上,成功解决:

@Component
@FeignClient(value = "service-vod",fallback = VodFallback.class)
public interface VodFeign {
    @DeleteMapping(value = "/vod/video/{id}")
    public Result removeVideo(@PathVariable("id") String videoId);

    @DeleteMapping(value = "/vod/video/delete-batch")
    public Result removeVideoList(@RequestParam("videoIdList") List<String> videoIdList);
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/openfeign%e4%bd%bf%e7%94%a8hystrix%e6%8a%a5%e9%94%99%ef%bc%9aambiguous-mapping-cannot-map-xx-method/

发表评论

电子邮件地址不会被公开。