JAX-RS/Jersey 简单字符串的路径参数正则表达式

JAX-RS/Jersey path parameter regex for a simple string

我正在尝试匹配字符串 v1v2。为此,我正在尝试以下正则表达式:^v(1|2)(我也尝试了 $,这可能是我需要的)。当我在 http://www.regextester.com/ 中测试它时,它似乎工作正常。但是当我在 JAX-RS 路径表达式中使用它时它不起作用。我使用的表达式如下:

@Path("/blah/{ver:^v(1|2)}/ep")

有没有我遗漏的特定于 JAX-RS 的内容?

尝试以下方法(无锚点):

@Path("/blah/{ver : v(1|2)}/ep")

此外,如果仅更改单个字符,请使用字符集而不是 | 运算符:

@Path("/blah/{ver : v[12]}/ep")

由于锚 ^,您的尝试无效。引用 JAX-RS specification, chapter 3.7.3(强调我的):

The function R(A) converts a URI path template annotation A into a regular expression as follows:

  1. URI encode the template, ignoring URI template variable specifications.
  2. Escape any regular expression characters in the URI template, again ignoring URI template variable specifications.
  3. Replace each URI template variable with a capturing group containing the specified regular expression or ‘([ˆ/]+?)’ if no regular expression is specified.
  4. If the resulting string ends with ‘/’ then remove the final character.
  5. Append ‘(/.*)?’ to the result.

因为每个 URI 模板都放在一个捕获组中,所以不能在其中嵌入锚点。

因此,以下将起作用并将匹配 v1v2

@Path("/blah/{ver:v[12]}/ep")