代码之家  ›  专栏  ›  技术社区  ›  AddJ

Spring@RequestMapping“不包含”Regex

  •  1
  • AddJ  · 技术社区  · 6 年前

    我有这个请求映射:

    @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 department”url实际上也与第二个请求映射匹配。。。

    @RequestMapping(value = "/route/to-{destination:^((?!-from-).)+}.html", method = {RequestMethod.GET, RequestMethod.HEAD})
    

    因此,如果“destination”包含“-from-”,则请求映射将不匹配。

    还有。。。它不起作用!第一个请求映射成功地提供了url“/route/to barcelona from paris.html”,但根本没有提供url“/route/to barcelona.html”。。。我错过了什么?

    注意:我不想使用java解决方案,例如使用单个“/route/to-{destination}”请求映射,然后检查“destination”是否包含“-from-”。。:)而且,我不能改变这些路线,因为搜索引擎优化。。。

    2 回复  |  直到 5 年前
        1
  •  2
  •   Wiktor Stribiżew    6 年前

    你可以用

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

    这个 ^ 锚点将搜索字符串的开头,并在此处失败任何匹配。

    这个 (?!.*-from-) 负展望将使任何包含 -from- 在除换行符以外的任何0+个字符之后。

    这个 .+ 模式将消耗除换行符以外的所有1个或多个字符到行尾。

        2
  •  0
  •   oks    6 年前

    {目的地:^(?!从)+}

    {目的地:^((?!-从-)+}