出现java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'xxx' method
异常。
通过上面代码我们可以看出来当spring添加Controller的接口Mapping的时候会先进行效验,如果以存在相同的Mapping了,并且方法来源不是同一个类,那么就会报错
比如:
//解决重写Controller, 方法参数返回值不一致的问题,
//解决办法就是如果子类中有相同路径的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)) { //发现有重复的
//删除原来的
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();
}
}
点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_45203607/article/details/123729494
内容来源于网络,如有侵权,请联系作者删除!