我正在尝试构建一个请求过滤器,该过滤器只有在匹配字母 e 的模式时才会使用,然后是数字。但是我似乎无法让它工作。每次尝试使用正则表达式时,我都会收到 400 个错误。
如果我只使用以下内容,它会“工作”,但也会捕获没有我不想要的数字的映射。
@RequestMapping(value = "e{number}",
method = RequestMethod.GET)
我尝试了以下组合。
@RequestMapping(value = "e{number}",
params = "number:\\d+",
method = RequestMethod.GET)
@RequestMapping(value = "e{number:\d+}",
method = RequestMethod.GET)
@RequestMapping(value = "/e{^\\+?\\d+\$}",
method = RequestMethod.GET)
@RequestMapping(value = "/{^\\e+?\\d+\$}",
method = RequestMethod.GET)
最佳答案
根据the documentation ,你必须使用类似 {varName:regex}
的东西。甚至还有一个例子:
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(@PathVariable String version, @PathVariable String extension) {
// ...
}
}
关于java - Spring Controller 中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422368/