【问题标题】:How to match only paths in /[^/]+ with Spring @RequestMapping?如何仅将 /[^/]+ 中的路径与 Spring @RequestMapping 匹配?
【发布时间】:2019-08-21 04:07:38
【问题描述】:

我需要以下物品:

任何请求

等等,应该转发给

https://localhost:8443/a/web/index.html

现在,我就是这样做的:

@Controller
public class ForwardController {
    @RequestMapping(value = "/*", method = RequestMethod.GET)
    public String redirectRoot(HttpServletRequest request) {
        return "forward:/a/web/index.html";
    }
}

问题是:

这也匹配 https://localhost:8443/api/(注意末尾的 /)。

这是一个问题,因为这是我希望 Spring Data REST 基本路径所在的位置:

spring.data.rest.base-path=/api/

/api != /api/ 涉及 REST 端点。

什么应该有效,但不知何故没有

我尝试了几种不同的正则表达式,但仍然无法完成我想要的。例如(demo):

@RequestMapping(value = "/[^/]+", method = RequestMethod.GET)

现在将适用于 Spring Data - 我获得了我期望的所有资源信息,但访问 https://localhost:8443/ 现在已损坏,无法再访问 Web 客户端。

同样的道理

@RequestMapping(value = "/{path}", method = RequestMethod.GET)
@RequestMapping(value = "/{path:[^/]+}", method = RequestMethod.GET)

其行为类似于/*(也匹配下一个/)。

这个问题已经困扰了我好几个星期,但仍然没有解决方案的见解。


整个问题也可以看成:

为什么"/[^/]+" 不匹配https://localhost:8443/whatever

【问题讨论】:

标签: regex spring spring-boot spring-data-jpa


【解决方案1】:

Regex 通常不是最快的尝试。 您可以将路径列表发送到

 @RequestMApping(value={"", "/", "/test", "/api"},  method = RequestMethod.GET)

Multiple Spring @RequestMapping annotations

【讨论】:

  • 抱歉,这不是我正在寻找的通用解决方案。
猜你喜欢
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-10
相关资源
最近更新 更多