feign Ambiguous mapping 解决方式
Feign Ambiguous mapping 解决方式
背景
feign在使用过程中,一般先定义接口,再定义具体实现的controller,如下所示
@FeignClient(name = "shore-app")
public interface StoreApi {@GetMapping("/api/store/getStoreNum")Integer getStoreNum();@GetMapping("/api/store/getStoreNumByCondition1")Integer getStoreNumByCondition1();@GetMapping("/api/store/getStoreNumByCondition2")Integer getStoreNumByCondition2();
}
实现定义
@RestController
public class StoreController implements StoreApi {@Overridepublic Integer getStoreNum() {return Integer.MAX_VALUE;}@Overridepublic Integer getStoreNumByCondition1() {return Integer.MAX_VALUE;}@Overridepublic Integer getStoreNumByCondition2() {return Integer.MAX_VALUE;}
}
这种方式去提供Feign给其他模块调用也是比较简洁的,不过在定义controller
很多人做法是把url公共的部分抽取出来,如下
@FeignClient(name = "shore-app")
@RequestMapping("/api/store")
public interface StoreApi {@GetMapping("/getStoreNum")Integer getStoreNum();@GetMapping("/getStoreNumByCondition1")Integer getStoreNumByCondition1();@GetMapping("/getStoreNumByCondition2")Integer getStoreNumByCondition2();
}
然后会发现项目启动报错,有相同的url路径,启动不起来,报错如下
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 'org.github.hoorf.store.api.StoreApi' method
org.github.hoorf.store.api.StoreApi#getStoreNumByCondition2()
to {GET /api/store/getStoreNumByCondition2}: There is already 'storeController' bean method
org.github.hoorf.store.controller.StoreController#getStoreNumByCondition2() mapped.at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
报错原因是@RequestMapping
标注上,被spring识别为controller
本文介绍的是一种新的feign定义模式去解决这问题,既能保持原有的controller
使用习惯,又能将feign定义的接口复用到其他项目
实现
工程目录分包依赖情况
e1
|--e1-api
| |--e1-store-api
| |--e1-account-api
| |--e1-common
|--e1-acount
|--e1-store
其中e1-account
,e1-store
分别依赖e1-api
,e1-api
为所有api的聚合依赖
- 自定义Feign注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@FeignClient
@Conditional(AdaptiveFeignCondition.class)
public @interface AdaptiveFeign {@AliasFor(annotation = FeignClient.class)String value() default "";@AliasFor(annotation = FeignClient.class)Class<?> fallback() default void.class;@AliasFor(annotation = FeignClient.class)Class<?> fallbackFactory() default void.class;
}
- 定义注解启用条件
基本逻辑就是,如果识别到实现类有controller则不启用Feign注解,识别不到则启用Feign注解
@Slf4j
public class AdaptiveFeignCondition extends SpringBootCondition {@Overridepublic ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {ClassMetadata classMetadata = (ClassMetadata) metadata;String className = classMetadata.getClassName();Class interfaceClass = null;try {interfaceClass = Class.forName(className);} catch (ClassNotFoundException e) {}Set<Class<?>> classSet = ReflectionUtils.getSubTypesOf(interfaceClass);for (Class<?> implClass : classSet) {Controller annotation = AnnotationUtils.findAnnotation(implClass, Controller.class);if (null != annotation) {// log.debug("{} exits Controller.class ,no init feign {}", implClass.getName(), interfaceClass.getName());return ConditionOutcome.noMatch(new StringBuffer(implClass.getName()).append(" exits implement").toString());}}return ConditionOutcome.match();}
}
效果
在 account
模块中使用
@Autowiredprivate AccountApi accountApi;
注入的是Spring中的AccountApi
具体实现,也就是controller,走的是内部调用
在 store
模块中使用
注入的是AccountApi
Feign的实现,走的是远程调用
具体工程代码https://github.com/hoorf/arch/tree/master/arch-example-1
feign Ambiguous mapping 解决方式相关推荐
- Ambiguous mapping. Cannot map *** method 报错解决
开发中,我们常常会遇到很多异常报错,现在就我工作中经常遇到的报错做记录和总结,首先对自己会有很大的帮助,同时希望对读者也起到一定的帮助.废话不多说,先上报错. 1. Ambiguous mapping ...
- pringMVC“Ambiguous mapping found. Cannot map ‘XXXController‘ bean method”解决方法
pringMVC"Ambiguous mapping found. Cannot map 'XXXController' bean method"解决方法 参考文章: (1)pri ...
- pringMVC“Ambiguous mapping found. Cannot map 'XXXController' bean method”解决方法
[转 :http://www.fanfanyu.cn/news/staticpagefile/2351.html] 最近在开发项目的过程中SpringMVC抛了个"Ambiguous map ...
- 映射报错怎么解决 Ambiguous mapping. Cannot map ‘basicPersonStreamDataController‘ method
Ambiguous mapping. Cannot map 'basicPersonStreamDataController' method 出错首先检查 实现接口有没有@service注解 或者是C ...
- 【Spring Cloud】EnableFeignClients后报错:Ambiguous mapping. Cannot map XXX method YYY
这是在使用Feign优化Http调用时遇到的一个错误,启动时报错类似下方: Ambiguous mapping. Cannot map 'XXX' method YYY to {GET /file/x ...
- java.lang.IllegalStateException: Ambiguous mapping found. Cannot map ' ' bean method
问题:springmvc 启动时出现Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot 原因:控制层 ...
- Eclipse报:Ambiguous mapping. Cannot map 'XXXController' method
1.起因 今天写代码有点粗心了,把Controller中的代码移动到其他Controller下,忘了注释原来的代码,导致这个bug...eclipse报:Ambiguous mapping. Cann ...
- JAVA Web项目中所出现错误及解决方式合集(不断更新中)
JAVA Web项目中所出现错误及解决方式合集 前言 一.几个或许会用到的软件下载官网 二.Eclipse的[preferences]下没有[sever]选项 三.Tomcat的安装路径找不到 四.T ...
- sqlserver导入excel的电话号码(身份证)变为科学计数解决方式
如果excel中有一列存的是手机号码或者身份证号码,那么导入到sql中时,会把手机或者身份证当作数字格式对待,因而会以科学记数法的形式存在sqlserver表中,解决方式,先将excel文件另存为文本 ...
- 服务器自动post,jquery ajax $.post自动变GET的解决方式(for CI)
环境:CI 3.x + windows 2008 + phpstudy + jquery 1.7.2 + apache 以往一直用lnmp,这次用windows+apache,遇到了一些问题,记录下来 ...
最新文章
- Linux下vi编辑器命令精华版
- 数据结构 图的深度优先遍历 C
- [ATC 17] StreamBox: 面向多核机器上的针对Records的无序到达的实时流处理系统
- Integer类对象池与==问题:Integer a=34556,b=34556;但a==b为false
- Docker 三剑客
- python程序如何封装成接口_python接口自动化如何封装获取常量的类
- android UI自动化测试工具Robotium VS NativeDriver VS Calabash
- 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集...
- css用一张大图片来设置背景的技术真相
- JAVA中的toString()方法的用法
- python wget安装_Macbook系统环境安装wget的2个方法 - 传统包及Homebrew安装
- 小米集团架构调整:王川出任CSO 李肖爽兼任大家电部总经理
- cPanel设置自定义404错误页
- 线性代数的本质-20160614总结
- poj 3211 Washing Clothes
- java变量的声明和数据类型
- 几种segue的使用场景
- operate new、delete new和placement new
- codeforces 158E. Phone Talks(dp)
- CSS3炫酷模糊发光文字动画js特效
热门文章
- mzy git学习,删除文件(三)
- idcnd传媒官方专业提供
- 系统win8 任务栏消失不见的解决办法
- 笔记本Win10 装 ubuntu 20.04双系统踩坑记录:装Ubuntu系统+装显卡驱动(华硕天选2 配置intel i9+ RTX 3060+1T固态硬盘(两块512G固态硬盘))
- 【sketchup 2021】草图大师图像输出与渲染之Enscape渲染(优秀的实时渲染软件)的高级使用【灯光的添加、代理模型的添加、材质编辑器、视频编辑器、全景导出并编辑】
- linux bt下载没速度慢,linux bt速度之王
- html背景图片自动铺满屏幕,【CSS背景图片页面自适应充满屏幕】
- 0xc000007b的解决办法(续)
- cpu性能测试软件 国际象棋,国际象棋测试
- P5339 [TJOI2019]唱、跳、rap和篮球