Java 匹配以特定路径元素开头的任何url

Java 匹配以特定路径元素开头的任何url,java,regex,spring,spring-mvc,spring-boot,Java,Regex,Spring,Spring Mvc,Spring Boot,我使用的是Spring Boot v1.3.3.RELEASE,我想编写一个web服务,它可以映射到以下任何一项: /福 /foo/a /foo/a/b /foo/a/b/c /巴/日 /巴/德比 因此,基本上,任何以/foo或/bar开头的内容都可以在后面添加任意数量的路径元素 我的想法是: @RequestMapping("/{path:(foo|bar)(/[^/]+?)*}") @ResponseBody public String doStuff(final String pat

我使用的是Spring Boot v1.3.3.RELEASE,我想编写一个web服务,它可以映射到以下任何一项:

  • /福
  • /foo/a
  • /foo/a/b
  • /foo/a/b/c
  • /巴/日
  • /巴/德比
因此,基本上,任何以
/foo
/bar
开头的内容都可以在后面添加任意数量的路径元素

我的想法是:

@RequestMapping("/{path:(foo|bar)(/[^/]+?)*}")
@ResponseBody
public String doStuff(final String path) throws Exception
{
    // Do stuff with path
}
@RequestMapping({"/foo/**", "/bar/**"})
@ResponseBody
public String doStuff(HttpServletRequest request) throws Exception
{
    String path = request.getRequestURI();
    // Do stuff with path
}

但它似乎不起作用。有什么想法吗?

您不能在
路径变量中使用多个路径段,但您可以编写如下内容:

@RequestMapping("/{path:(foo|bar)(/[^/]+?)*}")
@ResponseBody
public String doStuff(final String path) throws Exception
{
    // Do stuff with path
}
@RequestMapping({"/foo/**", "/bar/**"})
@ResponseBody
public String doStuff(HttpServletRequest request) throws Exception
{
    String path = request.getRequestURI();
    // Do stuff with path
}

不能在
路径变量中使用多个路径段,但可以编写如下内容:

@RequestMapping("/{path:(foo|bar)(/[^/]+?)*}")
@ResponseBody
public String doStuff(final String path) throws Exception
{
    // Do stuff with path
}
@RequestMapping({"/foo/**", "/bar/**"})
@ResponseBody
public String doStuff(HttpServletRequest request) throws Exception
{
    String path = request.getRequestURI();
    // Do stuff with path
}

干杯,朋友。谢谢你的帮助。干杯,朋友。谢谢你的帮助。