Java Spring@RequestMapping";“不包含”;正则表达式
我有这样的要求:Java Spring@RequestMapping";“不包含”;正则表达式,java,regex,spring,request-mapping,Java,Regex,Spring,Request Mapping,我有这样的要求: @RequestMapping(value = "/route/to-{destination}-from-{departure}.html", method = {RequestMethod.GET, RequestMethod.HEAD}) @RequestMapping(value = "/route/to-{destination}.html", method = {RequestMethod.GET, RequestMethod.HEAD}) 我想补充一点,那就是
@RequestMapping(value = "/route/to-{destination}-from-{departure}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
@RequestMapping(value = "/route/to-{destination}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
我想补充一点,那就是请求映射:
@RequestMapping(value = "/route/to-{destination}-from-{departure}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
@RequestMapping(value = "/route/to-{destination}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
因此,它可以服务于所有“不出发”路线。然而,这会造成冲突,因为“/route/to destination from exchange”url实际上也与第二个RequestMapping匹配。。。
很公平,所以我的解决方案是指定一个正则表达式:
@RequestMapping(value = "/route/to-{destination:^((?!-from-).)+}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
因此,如果“目的地”包含“-from-”,则RequestMapping将不匹配
而且。。。它不起作用!url“/route/to barcelona from paris.html”由第一个RequestMapping成功提供,但url“/route/to barcelona.html”根本不提供。。。我错过了什么
注意:我不想使用java解决方案,例如使用单个“/route/to-{destination}”请求映射,然后检查“destination”是否包含“-from-”。:)此外,由于搜索引擎优化,我无法更改这些路线…尝试使用此:->
{目的地:^(?!from)+}
或
{目的地:^((?!-from-)+}您可以使用
"/route/to-{destination:(?!.*-from-).+}.html"
^
锚点将搜索字符串的开头,并将在此处失败任何匹配
(?!.-from-
负前瞻将使任何包含-from-
的输入在任何0+字符(换行字符除外)之后失败
+
模式将消耗除换行符以外的所有1个或多个字符到行尾。尝试{destination:(?!.-from-)+}.html
或{destination:(?!.-from-[^\/]+}.html
@WiktorStribiżew{destination:(?!.-from-)+}!你让我开心,非常感谢!(“{目的地:(?!.-from-[^\/]+}”没有)作为奖励,这里有一个关于你的漫画哈哈:两个都试过了,但都没用!Wiktor Stribiżew的“{destination:(?!.-from-).+}.html”起作用了,所以我们有了解决方案!谢谢你的帮助!