Spring @RequestMapping“不包含”正则表达式

我有此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})

因此,如果“目标”包含“ -from-”,则RequestMapping将不匹配。

而且…不起作用!网址“ /route/to-barcelona-from-

paris.html”已成功通过第一个RequestMapping提供,但网址“ /route/to-

barcelona.html”却根本未提供…我缺少什么?

注意:我不想使用Java解决方案,例如只有一个“ / route / to- {destination}” RequestMapping,然后检查“

destination”是否包含“ -from-” .. :)另外,我也无法更改这些路线是因为SEO …

回答:

您可以使用

"/route/to-{destination:(?!.*-from-).+}.html"

^主播将搜索字符串的开始,并将在此处失败的任何比赛。

(?!.*-from-)排除模式将失败包含任何输入-from-比换行符字符以外的任何字符0+后。

.+模式将消耗除行换行符之外的所有1个或更多字符。

以上是 Spring @RequestMapping“不包含”正则表达式 的全部内容, 来源链接: utcz.com/qa/421867.html

回到顶部