【发布时间】:2019-01-30 21:34:39
【问题描述】:
我有这个 RequestMapping:
@RequestMapping(value = "/route/to-{destination}-from-{departure}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
我想添加那个 RequestMapping:
@RequestMapping(value = "/route/to-{destination}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
因此它可以服务于所有“无出发”路线。但是,这会产生冲突,因为“/route/to-destination-from-departure”url 实际上也匹配第二个 RequestMapping ...... 很公平,所以我的解决方案是指定一个正则表达式:
@RequestMapping(value = "/route/to-{destination:^((?!-from-).)+}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
如果“destination”包含“-from-”,则RequestMapping将不匹配。
而且...它不起作用!第一个 RequestMapping 成功提供了 URL“/route/to-barcelona-from-paris.html”,但根本没有提供 URL“/route/to-barcelona.html”......我错过了什么?
注意:我不想使用 java 解决方案,例如使用单个“/route/to-{destination}”RequestMapping 然后检查“destination”是否包含“-from-”.. :) 另外,我可以不要因为 SEO 而改变那些路线...
【问题讨论】:
-
试试
{destination:(?!.*-from-).+}.html或{destination:(?!.*-from-)[^\/]+}.html。 -
@WiktorStribiżew "{destination:(?!.*-from-).+}" 有效!你让我很开心,非常感谢! (“{destination:(?!.*-from-)[^\/]+}”没有)作为奖励,这里有一个关于你的片段哈哈:commitstrip.com/en/2014/02/24/coder-on-the-verge-of-extinction
标签: java regex spring request-mapping