Feign请求java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'xxxService' bean method

最近项目中是fegin,同时也使用pandora,当把pandora去掉,项目无法启动:

Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [com/xxx/config/WebMvcConfiguration.class]: 
Invocation of init method failed; nested exception is java.lang.IllegalStateException: 
Ambiguous mapping. Cannot map 'xxx.point.IPointAccountReadServiceClient' method 
public abstract xxx.vo.PointDepartmentAccountVo xxxx.PointAccountReadService.getDepartmentAccountByCondition(xxxx.dto.PointDepartmentAccountQueryDto)
to {POST /api/v1/point/feign/acount/getDepartmentAccountByCondition}: There is already 'com.xxxx.PointAccountReadService' bean method
public abstract com.xxxx.account.vo.PointDepartmentAccountVo com.xxx.PointAccountReadService.getDepartmentAccountByCondition
  (com.xxxx.account.dto.PointDepartmentAccountQueryDto) mapped.

从上面异常信息看出是getDepartmentAccountByCondition,方法重复,导致报错,百度一圈,基本上都是这个原因,说@RequestMapping(名称)这里重复了。
仔细检查确实没有重复,怀疑是不是扫描feign时,是不是重复扫描了?

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(scanBasePackages = {"xyz.yxiumei.point","xyz.yxiumei.item","xyz.yxiumei.user"})
public class WebApplication {
}

通过代码搜寻,这些路径下PointAccountReadService也没有重复。然而和它没有半毛钱关系。
问题原因:

@FeignClient("point")
public interface IPointAccountReadService extends PointAccountReadService{}

自己写了接口继承提供方的接口,提供当的接口是:

@FeignClient("point")
public interface PointAccountReadService{
  @GetMapper("/getDepartmentAccountByCondition")
  Response<AccountInfo> getDepartmentAccountByCondition();
}

上面可以发现提供服务方都已经自己加了一个@FeignClient(“point”),然后消费方继承又加了@FeignClient(“point”),这里出现重复。
解决办法:直接把自己的写的接口删除,注入提供方的PointAccountReadService,直接使用就可以了

总结: 最初生效的原因是EnableFeignClients,没有指定扫描路径,对spring cloud还学要多了解。

你可能感兴趣的:(随笔)