博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc 3.2 @MatrixVariable bug 2
阅读量:7234 次
发布时间:2019-06-29

本文共 3672 字,大约阅读时间需要 12 分钟。

hot3.png

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

 

问题:

 

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

 上面代码是spring文档中的例子,浏览器中输入:时,控制台输出:

 

{q=[22, 33], s=[23]}  {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));	}

 

 

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

 

接下来看下springmvc如何进行url匹配的,假设此处使用的是RequestMappingHandlerMapping():

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,此处有说明:

 

接着执行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
matrixVars, @MatrixVariable(pathVar = "petId") Map
petMatrixVars) { System.out.println(matrixVars); System.out.println(petMatrixVars); }

 

已提交给springsource。

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

你可能感兴趣的文章
win7方面API學習
查看>>
mongodb 安装
查看>>
ubuntu bless 16字节每行
查看>>
TestNG 七 annotation
查看>>
mysql数据库管理工具(navicat for mysql)
查看>>
JVM基础(1)——内存模型
查看>>
程序员嘛,先做个好架构师再说
查看>>
用外挂只为“吃鸡”成功?为什么不试试正当手段!
查看>>
2017线上超市食品消费盘点:老字号成新网红 年轻人爱新奇特
查看>>
BATJ等公司必问的8道Java经典面试题,你都会了吗?
查看>>
开学季学生宿舍竟然限电,学校管理因噎废食?
查看>>
同样是异形屏,为什么OPPO R15能够打造不一样的体验?
查看>>
奇点汽车回应欠薪3月传闻:多轮融资顺利 不存在资金问题
查看>>
孕妇高速上产女 交警医生合力架起生命绿色通道
查看>>
西藏尼阿底遗址项目获“2018年中国考古新发现”入围奖
查看>>
2018年浙江检察机关办理公益诉讼案件5551件
查看>>
加拿大渥太华民众寒冬享受运河滑冰道乐趣
查看>>
火箭队再遭伤病打击 曝中锋卡佩拉至少缺阵1月
查看>>
和妈妈在一起,就是团圆
查看>>
程序员面试,写个javascript物理引擎,张口要月薪20K!面试官真给了!
查看>>