Java中Controller引起的Ambiguous?mapping問(wèn)題及解決
Controller引起的Ambiguous mapping問(wèn)題
問(wèn)題描述
出現(xiàn)java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'xxx' method異常。
通過(guò)上面代碼我們可以看出來(lái)當(dāng)spring添加Controller的接口Mapping的時(shí)候會(huì)先進(jìn)行效驗(yàn),如果以存在相同的Mapping了,并且方法來(lái)源不是同一個(gè)類,那么就會(huì)報(bào)錯(cuò)
比如:
- 子類繼承父類的Controller的方法,url都一樣
- 兩個(gè)不同類的Controller內(nèi)的方法url地址都一樣,但是方法行為都不同(名稱.參數(shù),返回值…)
- 總結(jié):只要出現(xiàn)相同的url接口就會(huì)報(bào)錯(cuò)
解決辦法
- 重寫RequestMappingHandlerMapping的getMappingForMethod方法。
- 判斷是準(zhǔn)備注冊(cè)的Mapping是否以存在
- 如果存在那么就將原來(lái)的Mapping刪除使用現(xiàn)在的Mapping
代碼
//解決重寫Controller, 方法參數(shù)返回值不一致的問(wèn)題, //解決辦法就是如果子類中有相同路徑的url接口那么就不映射父類的url接口了 public class PathTweakingRequestMappingHandlerMapping extends RequestMappingHandlerMapping { //handlerType.equals(ParentclassController.class) || handlerType.equals(SubclassController.class) @Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo methodMapping = super.getMappingForMethod(method, handlerType); if (methodMapping==null) { return methodMapping; } Map<RequestMappingInfo, HandlerMethod> handlerMethods = super.getHandlerMethods(); for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet()) { for (String pattern : requestMappingInfoHandlerMethodEntry.getKey().getPatternsCondition().getPatterns()) { for (String s : methodMapping.getPatternsCondition().getPatterns()) { if (pattern.equals(s)) { //發(fā)現(xiàn)有重復(fù)的 //刪除原來(lái)的 super.unregisterMapping(requestMappingInfoHandlerMethodEntry.getKey()); return null; } } } } return methodMapping; } }
package com.schemautils.config; import com.schemautils.PathTweakingRequestMappingHandlerMapping; import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations; import org.springframework.context.annotation.Configuration; @Configuration public class WebMvcRegistrationsConfig implements WebMvcRegistrations { @Override public PathTweakingRequestMappingHandlerMapping getRequestMappingHandlerMapping() { return new PathTweakingRequestMappingHandlerMapping(); } }
Ambiguous mapping(模糊映射)
小白的報(bào)錯(cuò)日常
Ambiguous mapping
Ambiguous mapping. Cannot map 'customerController' method
public com.cdmtc.model.CommonResult com.cdmtc.controller.CustomerController.insert(com.cdmtc.model.Customer)
to {[/insert],methods=[POST]}: There is already 'baseInfoController' bean method
public org.springframework.http.ResponseEntity<com.cdmtc.model.modelui.ResponseResult> com.cdmtc.controller.BaseInfoController.insert(com.cdmtc.model.BaseInfo) mapped.
有道翻譯如下:
模糊映射。無(wú)法映射“customerController”方法
公共com.cdmtc.model.CommonResult com.cdmtc.controller.CustomerController.insert (com.cdmtc.model.Customer)
對(duì)于{[/insert],methods=[POST]}:已經(jīng)有了’baseInfoController’ bean方法
公共org.springframework.http.ResponseEntity < com.cdmtc.model.modelui。ResponseResult > com.cdmtc.controller.BaseInfoController.insert (com.cdmtc.model.BaseInfo)映射。
原因:
有value值重復(fù)的PostMapping
在controller 找的結(jié)果如下
@PostMapping(value = "/insert") ?? ?public ResponseEntity<ResponseResult> insert(@RequestBody @ApiParam(name="基礎(chǔ)數(shù)據(jù)對(duì)象", type="BaseInfo", value="傳入json格式", required=true) BaseInfo baseInfo)? ?? ?@PostMapping(value = "/insert") @ApiOperation(value = "插入數(shù)據(jù)") ? ? public CommonResult insert(@RequestBody Customer customer)?
解決辦法
修改contoller 下 value 的值 ,讓他們不一樣就可以解決啦
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot之如何正確、安全的關(guān)閉服務(wù)
這篇文章主要介紹了SpringBoot之如何正確、安全的關(guān)閉服務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java中BigInteger與BigDecimal類用法總結(jié)
在Java中有兩個(gè)用于大數(shù)字運(yùn)算的類,分別是java.math.BigInteger類 和 java.math.BigDecimal類,這兩個(gè)類都可以用于高精度計(jì)算,BigInteger類是針對(duì)整型大數(shù)字的處理類,而BigDecimal類是針對(duì)大小數(shù)的處理類,接下來(lái)帶大家來(lái)學(xué)習(xí)一下,在Java中如何處理大數(shù)字2023-05-05JUC三大輔助類CountDownLatch、CyclicBarrier和Semaphore詳解
這篇文章主要介紹了JUC三大輔助類CountDownLatch、CyclicBarrier和Semaphore詳解,CountDownLatch 類可以設(shè)置一個(gè)計(jì)數(shù)器,然后通過(guò) countDown 方法來(lái)進(jìn)行 減 1 的操作,使用 await 方法等待計(jì)數(shù)器不大于 0,然后繼續(xù)執(zhí)行 await 方法 之后的語(yǔ)句,需要的朋友可以參考下2024-01-01IDEA中WebService生成Java代碼并調(diào)用外部接口實(shí)現(xiàn)代碼
這篇文章主要介紹了IDEA中WebService生成Java代碼并調(diào)用外部接口實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Java?IO模型之BIO、NIO、AIO三種常見IO模型解析
這篇文章主要介紹了今天我們來(lái)聊Java?IO模型,BIO、NIO、AIO三種常見IO模型,我們從應(yīng)用調(diào)用的過(guò)程中來(lái)分析一下整個(gè)IO的執(zhí)行過(guò)程,不過(guò)在此之前,我們需要簡(jiǎn)單的了解一下整個(gè)操作系統(tǒng)的空間布局,需要的朋友可以參考下2024-07-07java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例
這篇文章主要介紹了java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10詳解使用Java代碼讀取并比較本地兩個(gè)txt文件區(qū)別
這篇文章主要為大家介紹了使用Java代碼讀取并比較本地兩個(gè)txt文件區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java基于socket實(shí)現(xiàn)的客戶端和服務(wù)端通信功能完整實(shí)例
這篇文章主要介紹了Java基于socket實(shí)現(xiàn)的客戶端和服務(wù)端通信功能,結(jié)合完整實(shí)例形式分析了Java使用socket建立客戶端與服務(wù)器端連接與通信功能,需要的朋友可以參考下2018-05-05java基于正則表達(dá)式實(shí)現(xiàn)時(shí)間日期的常用判斷操作實(shí)例
這篇文章主要介紹了java基于正則表達(dá)式實(shí)現(xiàn)時(shí)間日期的常用判斷操作,簡(jiǎn)單說(shuō)明了正則表達(dá)式常用元字符含義并結(jié)合實(shí)例形式分析了java基于正則表達(dá)式針對(duì)常用日期時(shí)間格式的判斷操作技巧,需要的朋友可以參考下2017-10-10