简体   繁体   中英

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

I am trying to match strings v1 and v2 . For that, I am trying the following regex : ^v(1|2) (I also tried with $ which is probably what I need). When I test it in http://www.regextester.com/ , it seems to work fine. But when I used it in JAX-RS path expression it doesn't work. The expression I use is below:

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

Is there anything specific to JAX-RS that I am missing?

Your attempt does not work because of the anchor ^ . Quoting from the JAX-RS specification, chapter 3.7.3 (emphasis mine):

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.

Because each URI templates is placed inside a capturing group, you can't embed anchors in it.

As such, the following will work and will match v1 or v2 :

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

Try the following (without anchors):

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

Also, if the change is a single character only, use character set instead of the | operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM