Java中Controller引起的Ambiguous?mapping問題及解決
Controller引起的Ambiguous mapping問題
問題描述
出現(xiàn)java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'xxx' method異常。


通過上面代碼我們可以看出來當(dāng)spring添加Controller的接口Mapping的時(shí)候會(huì)先進(jìn)行效驗(yàn),如果以存在相同的Mapping了,并且方法來源不是同一個(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是否以存在
- 如果存在那么就將原來的Mapping刪除使用現(xiàn)在的Mapping
代碼
//解決重寫Controller, 方法參數(shù)返回值不一致的問題,
//解決辦法就是如果子類中有相同路徑的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ù)的
//刪除原來的
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.
有道翻譯如下:
模糊映射。無法映射“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實(shí)現(xiàn)文件上傳下載功能小結(jié)
最近做的一個(gè)項(xiàng)目涉及到文件上傳與下載功能。SpringBoot后臺(tái)如何實(shí)現(xiàn)文件上傳下載呢?下面有單文件上傳和多文件上傳功能,感興趣的朋友一起看看吧2017-08-08
springboot使用Redis隊(duì)列實(shí)戰(zhàn)
本文主要介紹了springboot使用Redis隊(duì)列實(shí)戰(zhàn),包含四種實(shí)現(xiàn)方式,基于List的 LPUSH+BRPOP的實(shí)現(xiàn), 基于Sorted-Set的實(shí)現(xiàn),PUB/SUB訂閱/發(fā)布模式和基于Stream類型的實(shí)現(xiàn),感興趣的可以了解一下2024-07-07
SpringBoot使用maven指定依賴包的版本(解決示例)
我們?cè)谑褂肁依賴的時(shí)候,這個(gè)依賴有引入了第三方B依賴,這時(shí)候我想指定B依賴的版本號(hào),下面?zhèn)€大家分享解決示例,對(duì)SpringBoot maven依賴包相關(guān)配置方法感興趣的朋友一起看看吧2024-04-04
基于Jenkins自動(dòng)打包并部署docker環(huán)境的操作過程
這篇文章主要介紹了基于Jenkins自動(dòng)打包并部署docker環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
Java學(xué)習(xí)常用包(類)之java.util包詳解
這篇文章主要介紹了Java學(xué)習(xí)常用包(類)之java.util包的相關(guān)資料,Java.util包是Java標(biāo)準(zhǔn)類庫的重要組成部分,包含集合框架、日期時(shí)間類、事件模型、隨機(jī)數(shù)生成器等實(shí)用工具類,集合框架提供了多種數(shù)據(jù)結(jié)構(gòu)和算法,需要的朋友可以參考下2024-10-10

