springmvc 3.2 @MatrixVariable bug 2

之前遇到过一个bug,《spring3.2 带matrix变量的URL匹配问题》(spring3.2.3已经修复该bug),今天看到问答又有人遇到一个,在此记录下,bug可真不少,测试用例看了下,写的并不是很全面。

 

问题:

http://www.iteye.com/problems/95247

 

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)  
    public void findPet(@MatrixVariable Map<String, String> matrixVars, @MatrixVariable(pathVar = "petId") Map<String, String> petMatrixVars) {  
        System.out.println(matrixVars);  
        System.out.println(petMatrixVars);  
    }

 上面代码是spring文档中的例子,浏览器中输入:http://localhost:8080/owners/44/pets/55;q=22,33;s=23时,控制台输出:

 

{q=[22, 33], s=[23]}  
{q=[22, 33], s=[23]}

但是当输入http://localhost:8080/owners/42;q=11;r=12/pets/55;q=22,33;s=23则findPet方法没有执行,难道spring文档有错误?还是少了什么配置?

 

目前不支持

 

分析:

1、先看下springmvc官网的单元测试用例

@Test
	public void getLookupPathWithSemicolonContent() {
		helper.setRemoveSemicolonContent(false);

		request.setContextPath("/petclinic");
		request.setServletPath("/main");
		request.setRequestURI("/petclinic;a=b/main;b=c/welcome.html;c=d");

		assertEquals("/welcome.html;c=d", helper.getLookupPathForRequest(request));
	}

	@Test
	public void getLookupPathWithSemicolonContentAndNullPathInfo() {
		helper.setRemoveSemicolonContent(false);

		request.setContextPath("/petclinic");
		request.setServletPath("/welcome.html");
		request.setRequestURI("/petclinic;a=b/welcome.html;c=d");

		assertEquals("/welcome.html;c=d", helper.getLookupPathForRequest(request));
	}

 https://github.com/SpringSource/spring-framework/blob/master/spring-web/src/test/java/org/springframework/web/util/UrlPathHelperTests.java

 

此处可以看到,如果请求的uri是/owners/42;q=11;r=12/pets/55;q=22,33;s=23,那么request.getServletPath()实际是/owners/42,而不是期待的/owners/42/pets/55;可以参考

https://java.net/jira/browse/SERVLET_SPEC-67?page=com.atlassian.streams.streams-jira-plugin%3Aactivity-stream-issue-tab

 

接下来看下springmvc如何进行url匹配的,假设此处使用的是RequestMappingHandlerMapping(http://jinnianshilongnian.iteye.com/blog/1682510):

1、

protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception {
		String lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
		if (logger.isDebugEnabled()) {
			logger.debug("Looking up handler method for path " + lookupPath);
		}

		HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);

 此处委托给UrlPathHelper去查找path

2、

public String getLookupPathForRequest(HttpServletRequest request) {
		// Always use full path within current servlet context?
		if (this.alwaysUseFullPath) {
			return getPathWithinApplication(request);
		}
		// Else, use path within current servlet mapping if applicable
		String rest = getPathWithinServletMapping(request);
		if (!"".equals(rest)) {
			return rest;
		}
		else {
			return getPathWithinApplication(request);
		}
	}

 此处因为默认不开启alwaysUseFullPath,因此会执行getPathWithinServletMapping()

3、

public String getPathWithinServletMapping(HttpServletRequest request) {
		String pathWithinApp = getPathWithinApplication(request);
		String servletPath = getServletPath(request);
		String path = getRemainingPath(pathWithinApp, servletPath, false);
		if (path != null) {
			// Normal case: URI contains servlet path.
			return path;
		}
		else {
			// Special case: URI is different from servlet path.
			// Can happen e.g. with index page: URI="/", servletPath="/index.html"
			// Use path info if available, as it indicates an index page within
			// a servlet mapping. Otherwise, use the full servlet path.
			String pathInfo = request.getPathInfo();
			return (pathInfo != null ? pathInfo : servletPath);
		}
	}

此处调用String servletPath = getServletPath(request); 出问题了,如果我们的url是 http://localhost:8080/owners/44;a=123/pets/55;q=22,33;s=23,那么获取的将是/owners/44,而不是/owners/42/pets/55,此处有说明:

https://java.net/jira/browse/SERVLET_SPEC-67?page=com.atlassian.streams.streams-jira-plugin%3Aactivity-stream-issue-tab

 

接着执行getRemainingPath则获取到剩余部分 /pets/55;q=22,33;s=23,即和之前的单元测试一样。 所以得到的path不可能匹配模式/owners/{ownerId}/pets/{petId},但是匹配/pets/{petId}(但没有意义了),所以失败了。

 

@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET)  
    public void findPet(@MatrixVariable Map<String, String> matrixVars, @MatrixVariable(pathVar = "petId") Map<String, String> petMatrixVars) {  
        System.out.println(matrixVars);  
        System.out.println(petMatrixVars);  
    }

 

已提交给springsource。

https://jira.springsource.org/browse/SPR-10577

转载于:https://my.oschina.net/qjx1208/blog/200934

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值