【问题标题】:Match any url which begins with a certain path element匹配任何以某个路径元素开头的 url
【发布时间】:2016-09-12 08:17:03
【问题描述】:

我正在使用 Spring Boot v1.3.3.RELEASE 并想编写一个可以映射到以下任何内容的 Web 服务:

  • /foo
  • /foo/a
  • /foo/a/b
  • /foo/a/b/c
  • /bar/d
  • /bar/d/e
  • ...

所以基本上任何以/foo/bar 开头的东西,然后可以是任意数量的路径元素。

我在想这样的事情:

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

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

【问题讨论】:

    标签: java regex spring spring-mvc spring-boot


    【解决方案1】:

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

    @RequestMapping({"/foo/**", "/bar/**"})
    @ResponseBody
    public String doStuff(HttpServletRequest request) throws Exception
    {
        String path = request.getRequestURI();
        // Do stuff with path
    }
    

    【讨论】:

    • 干杯朋友。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 2011-05-09
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多