Spring MVC controller路径是否能够重复?

背景

有一次面试,面试官问我同一个controller里面路径能不能重复,我斩钉截铁的回答不行,然后问我原因的时候我也不知道,最后面试官微微一笑然后就让我回去等通知了。 最近突然想到这个问题,然后就看了一下源码,在此记录一下。看过源码之后发现路径是可以重复的

实践出真知

先创建一个springboot项目,再创建一个TestController用于测试。
路径重复问题分为以下3种情况:

  1. 路径和请求方法都相同
  2. 路径和请求方法相同,但路径参数不同
  3. 路径相同,请求方法不同

以下分别来测试说明:

  1. 路径和请求方法都相同
@RestController
@RequestMapping("test")
public class TestController {

    @GetMapping("/test2")
    public String test1(){
        return "test1";
    }

    @GetMapping("/test2")
    public String test2(){
        return "test2";
    }
}
复制代码

启动的时候报错,这是因为路径完全相同了,肯定是不行的。 在这里插入图片描述

  1. 路径和请求方法相同,但路径参数不同
@RestController
@RequestMapping("test")
public class TestController {

    @GetMapping("/test1/{u1}")
    public String testMethod2(@PathVariable String u1){
        return "testMethod2="+u1;
    }

    @GetMapping("/test1/{u2}")
    public String testMethod1(@PathVariable String u2){
        return "testMethod1="+u2;
    }
}
复制代码

这种情况在启动的时候不报错,那通过访问测试看看会不会报错。 测试结果是报错了,查看异常信息跟上面是类似的。

  1. 路径相同,请求方法不同
@RestController
@RequestMapping("test")
public class TestController {

    @GetMapping("/test1")
    public String testMethod2(@PathVariable String u1){
        return "testMethod2="+u1;
    }

    @PostMapping("/test1")
    public String testMethod1(@PathVariable String u2){
        return "testMethod1="+u2;
    }
}
复制代码

这种情况在启动的时候不报错,那通过访问测试看看会不会报错,并且测试能正常通过

追根溯源

路径和请求方法都相同,异常源码追踪

第一种情况是启动时候的报错,通过查询报错信息找到了报错所在类的方法:org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#validateMethodMapping

private void validateMethodMapping(HandlerMethod handlerMethod, T mapping) {
			MappingRegistration<T> registration = this.registry.get(mapping);
			HandlerMethod existingHandlerMethod = (registration != null ? registration.getHandlerMethod() : null);
			if (existingHandlerMethod != null && !existingHandlerMethod.equals(handlerMethod)) {
				throw new IllegalStateException(
						"Ambiguous mapping. Cannot map '" + handlerMethod.getBean() + "' method \n" +
						handlerMethod + "\nto " + mapping + ": There is already '" +
						existingHandlerMethod.getBean() + "' bean method\n" + existingHandlerMethod + " mapped.");
			}
		}
复制代码

validateMethodMapping方法作用是判断某个方法的路径映射mapping是否在registry注册表中存在,如果存在并且跟当前的handlerMethod不同(bean或者method有一个不同就满足条件)。 HandlerMethod#equals方法:

@Override
	public boolean equals(@Nullable Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof HandlerMethod)) {
			return false;
		}
		HandlerMethod otherMethod = (HandlerMethod) other;
		return (this.bean.equals(otherMethod.bean) && this.method.equals(otherMethod.method));
	}
复制代码

由于第一种情况,两个方法所在的bean相同,但是method不同,所以启动的时候就抛异常了。

MappingRegistry

上面说的registry注册表是AbstractHandlerMethodMapping内部类MappingRegistry的一个变量。关于MappingRegistry 官方注释是这样的:

/**
	 * A registry that maintains all mappings to handler methods, exposing methods
	 * to perform lookups and providing concurrent access.
	 * <p>Package-private for testing purposes.
	 */
	class MappingRegistry {
复制代码

大概意思就是这个类的作用是维护映射的一个注册表并提供并发访问。

在MappingRegistry里面维护着一个Map<T, MappingRegistration> registry 注册表,MappingRegistry 有个register方法,启动的时候会把controller中方法的路径注册进来。

  class MappingRegistry {

		private final Map<T, MappingRegistration<T>> registry = new HashMap<>();

    //多个值的map,key重复的话会被value放到一个list集合中
		private final MultiValueMap<String, T> pathLookup = new LinkedMultiValueMap<>();
  
  public void register(T mapping, Object handler, Method method) {
      //获取锁
			this.readWriteLock.writeLock().lock();
			try {
        //创建一个方法映射处理器
				HandlerMethod handlerMethod = createHandlerMethod(handler, method);
        // 校验路径是否重复
				validateMethodMapping(handlerMethod, mapping);
        //获取路径上没有变量的路径
				Set<String> directPaths = AbstractHandlerMethodMapping.this.getDirectPaths(mapping);
        
				for (String path : directPaths) {
					this.pathLookup.add(path, mapping);
				}

				String name = null;
				if (getNamingStrategy() != null) {
					name = getNamingStrategy().getName(handlerMethod, mapping);
					addMappingName(name, handlerMethod);
				}

        //检查跨域配置
				CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping);
				if (corsConfig != null) {
					corsConfig.validateAllowCredentials();
					this.corsLookup.put(handlerMethod, corsConfig);
				}

        //mapping 注册到注册表中
				this.registry.put(mapping,
						new MappingRegistration<>(mapping, handlerMethod, directPaths, name, corsConfig != null));
			}
			finally {
        //释放锁
				this.readWriteLock.writeLock().unlock();
			}
		}
  }
复制代码

路径和请求方法相同,但路径参数不同,异常源码追踪

MappingRegistry属性 reistry注册表中key是RequestMappingInfo对象,RequestMappingInfo重写了equals方法

@Override
	public boolean equals(@Nullable Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof RequestMappingInfo)) {
			return false;
		}
		RequestMappingInfo otherInfo = (RequestMappingInfo) other;
		return (getActivePatternsCondition().equals(otherInfo.getActivePatternsCondition()) &&
				this.methodsCondition.equals(otherInfo.methodsCondition) &&
				this.paramsCondition.equals(otherInfo.paramsCondition) &&
				this.headersCondition.equals(otherInfo.headersCondition) &&
				this.consumesCondition.equals(otherInfo.consumesCondition) &&
				this.producesCondition.equals(otherInfo.producesCondition) &&
				this.customConditionHolder.equals(otherInfo.customConditionHolder));
	}
复制代码

由于第二种情况,路径和请求方法相同,但路径参数不同,也就是RequestMappingInfo对象中patternsCondition属性是不同的,所以在启动的时候不会报错。但是在查询路径匹配的时候会报错,报错位置在AbstractHandlerMethodMapping#lookupHandlerMethod方法中

protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
		List<Match> matches = new ArrayList<>();
  // 先查看不带参数的路径是否有匹配的
		List<T> directPathMatches = this.mappingRegistry.getMappingsByDirectPath(lookupPath);
		if (directPathMatches != null) {
			addMatchingMappings(directPathMatches, matches, request);
		}
    //如果在directPathMatches中没有找到匹配的,从注册表中查找
		if (matches.isEmpty()) {
      //从注册表中找到符合规则的路径,并储存到matches集合中
			addMatchingMappings(this.mappingRegistry.getRegistrations().keySet(), matches, request);
		}
		if (!matches.isEmpty()) {
			Match bestMatch = matches.get(0);
			if (matches.size() > 1) {
				Comparator<Match> comparator = new MatchComparator(getMappingComparator(request));
				matches.sort(comparator);
				bestMatch = matches.get(0);
				if (logger.isTraceEnabled()) {
					logger.trace(matches.size() + " matching mappings: " + matches);
				}
				if (CorsUtils.isPreFlightRequest(request)) {
					for (Match match : matches) {
						if (match.hasCorsConfig()) {
							return PREFLIGHT_AMBIGUOUS_MATCH;
						}
					}
				}
				else {
          // 路径第二选择
					Match secondBestMatch = matches.get(1);
          // 路径第一选择和第二选择比较,相同则抛异常
					if (comparator.compare(bestMatch, secondBestMatch) == 0) {
						Method m1 = bestMatch.getHandlerMethod().getMethod();
						Method m2 = secondBestMatch.getHandlerMethod().getMethod();
						String uri = request.getRequestURI();
						throw new IllegalStateException(
								"Ambiguous handler methods mapped for '" + uri + "': {" + m1 + ", " + m2 + "}");
					}
				}
			}
			request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, bestMatch.getHandlerMethod());
			handleMatch(bestMatch.mapping, lookupPath, request);
			return bestMatch.getHandlerMethod();
		}
		else {
			return handleNoMatch(this.mappingRegistry.getRegistrations().keySet(), lookupPath, request);
		}
	}
复制代码

lookupHandlerMethod方法的作用是为当前请求寻找最佳路径,先从没有路径路径参数的路径中查看是否有符合的,没有的话就从注册表中查找符合规则的路径,如果有多个路径符合规则就比较第一选择和第二选择是否相等,相等则抛异常,比较的对象也是RequestMappingInfo对象。

那请求进来的时候,路径的最优选择时怎么选择的呢?匹配规则在RequestMappingInfo的compareTo方法中,根据顺序进行匹配,匹配项最多的就是最好的选择。

public int compareTo(RequestMappingInfo other, HttpServletRequest request) {
		int result;
		// Automatic vs explicit HTTP HEAD mapping
		if (HttpMethod.HEAD.matches(request.getMethod())) {
			result = this.methodsCondition.compareTo(other.getMethodsCondition(), request);
			if (result != 0) {
				return result;
			}
		}
		result = getActivePatternsCondition().compareTo(other.getActivePatternsCondition(), request);
		if (result != 0) {
			return result;
		}
		result = this.paramsCondition.compareTo(other.getParamsCondition(), request);
		if (result != 0) {
			return result;
		}
		result = this.headersCondition.compareTo(other.getHeadersCondition(), request);
		if (result != 0) {
			return result;
		}
		result = this.consumesCondition.compareTo(other.getConsumesCondition(), request);
		if (result != 0) {
			return result;
		}
		result = this.producesCondition.compareTo(other.getProducesCondition(), request);
		if (result != 0) {
			return result;
		}
		// Implicit (no method) vs explicit HTTP method mappings
		result = this.methodsCondition.compareTo(other.getMethodsCondition(), request);
		if (result != 0) {
			return result;
		}
		result = this.customConditionHolder.compareTo(other.customConditionHolder, request);
		if (result != 0) {
			return result;
		}
		return 0;
	}
复制代码

路径相同,请求方法不同

对于第三种情况路径相同,请求方法不同,由于比较的RequestMappingInfo对象重写了equals方法,由于methodsCondition不同,所以最终还是可以重复不报错的。

总结

通过源码我们可以发现,controller中的方法但凡使用了@RequestMapping注解或者其派生注解(例如:@GetMapping、@PostMapping),其都会映射为一个RequestMappingInfo对象,比较路径是否重复主要还是看RequestMappingInfo对象的equals方法,所以上述说的第三种情况之后可重复路径的一种,请求方法重复。其实有很多种路径可重复的方式,只要在使用@RequestMapping注解及其派生注解时,注解中的属性不同都有可能路径重复,感兴趣的可以一一尝试,这里就不做过多测试了。

能力一般,水平有限,如有问题,请多指出。
更多文章可以关注一下我的微信公众号suncodernote

猜你喜欢

转载自juejin.im/post/7097078372899127310