最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中Controller引起的Ambiguous?mapping問題及解決

 更新時(shí)間:2022年10月27日 16:42:00   作者:胡安民-獨(dú)行者  
這篇文章主要介紹了Java中Controller引起的Ambiguous?mapping問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)備注冊的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é)

    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)

    本文主要介紹了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指定依賴包的版本(解決示例)

    SpringBoot使用maven指定依賴包的版本(解決示例)

    我們在使用A依賴的時(shí)候,這個(gè)依賴有引入了第三方B依賴,這時(shí)候我想指定B依賴的版本號(hào),下面?zhèn)€大家分享解決示例,對(duì)SpringBoot maven依賴包相關(guān)配置方法感興趣的朋友一起看看吧
    2024-04-04
  • Java利用過濾器實(shí)現(xiàn)完善登錄功能

    Java利用過濾器實(shí)現(xiàn)完善登錄功能

    這篇文章主要為大家詳細(xì)介紹了Java如何利用過濾器實(shí)現(xiàn)完善登錄功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-09-09
  • 基于Jenkins自動(dòng)打包并部署docker環(huán)境的操作過程

    基于Jenkins自動(dòng)打包并部署docker環(huán)境的操作過程

    這篇文章主要介紹了基于Jenkins自動(dòng)打包并部署docker環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Java使用continue語句的實(shí)例詳解

    Java使用continue語句的實(shí)例詳解

    這篇文章主要介紹了Java使用continue語句的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握使用方法,需要的朋友可以參考下
    2017-10-10
  • Java學(xué)習(xí)常用包(類)之java.util包詳解

    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
  • Java從List中刪除元素的幾種方式小結(jié)

    Java從List中刪除元素的幾種方式小結(jié)

    在Java中,List 接口提供了一個(gè) remove(Object o) 方法來移除列表中與給定對(duì)象相等的第一個(gè)元素,然而,直接使用這個(gè)方法來刪除列表中的元素有時(shí)并不是最優(yōu)的選擇,主要原因包括效率和同步性問題,本文介紹了Java從List中刪除元素的幾種方式,需要的朋友可以參考下
    2024-08-08
  • iBatis習(xí)慣用的16條SQL語句

    iBatis習(xí)慣用的16條SQL語句

    iBatis 是apache 的一個(gè)開源項(xiàng)目,一個(gè)O/R Mapping 解決方案,iBatis 最大的特點(diǎn)就是小巧,上手很快.這篇文章主要介紹了iBatis習(xí)慣用的16條SQL語句的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 簡述Java List去重五種方法

    簡述Java List去重五種方法

    這篇文章主要介紹了簡述Java List去重五種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01

最新評(píng)論

逊克县| 贵阳市| 常山县| 长岛县| 福清市| 定州市| 赣榆县| 泸西县| 交城县| 高唐县| 昭觉县| 天水市| 无锡市| 漳州市| 临沧市| 民丰县| 镇巴县| 同仁县| 陵水| 定兴县| 甘德县| 庐江县| 揭西县| 额敏县| 通州区| 石景山区| 稷山县| 武强县| 慈利县| 屯门区| 兴化市| 曲靖市| 海伦市| 甘洛县| 克什克腾旗| 海口市| 黄浦区| 沽源县| 如皋市| 长沙县| 安平县|