本文基于Spring 5.0+前端
项目某些页面是经过后台映射的,匹配映射路径时,有那种路径参数匹配。其中一个定义好的路径匹配是:web
/{corporationName}_{vendorName}/{shopName}_with_{retailer_name}?g={goodsId}
当时没有仔细看,其实{goodsId}
也应该做为路径参数,多是前端想要拿到这个id,放在url参数的位置比较容易拿到,因此就放在这个位置。spring
想验证下这样自由的url是否能匹配成功,看了下spring-webmvc
和spring-core
的源码,找到url匹配核心类PathMatcher
,目前仍是只有古老的AntPathMatcher
这一种实现。mvc
咱们来试下,是否能匹配成功:app
new AntPathMatcher().match("/{corporationName}_{vendorName}/{shopName}_with_{retailer_name}?g={goodsId}","/corporation 1 _ vendor 2/shop 1_with_retailer 2?g=good1")
这个返回是true
url
看来能够匹配,实装后,发现访问路径返回404.查看源码,发现HttpRequest的url里面是不带url参数的,诶呀,把这个给忘了,修改为 @RequestMapping("/{corporationName}_{vendorName}/{shopName}_with_{retailer_name}")
以后,成功设计
可是,仍是在设计的时候,把goodId设置在路径上面,比较好。虽然前端可能会麻烦一点儿code