Jax-RS : Regular Expressions in @Path Annotations

Sometimes I really wish I did something else for a living. Okay, perhaps a bit over-dramatic but I’m feeling a bit tired this evening and wasting even 20 minutes on something totally useless (which, let’s face it, isn’t unusual in computing) has me ready to call it a night. But since googling the error codes I was getting netted nothing (other than this 2 year old thread), perhaps I can be of service to the next person bitten by this.

I’m writing a RESTful web service, using Jax-RS with a CXF runtime (v 2.2.10). I’m defining a resource with a URI something like /foo/bar/… – that is, the first part of my uri will have the literals “foo” and “bar” and then I want everything else on the URI to go into a parameter. So I want /foo/bar/a, /foo/bar/a/b and /foo/bar/ab/c to all resolve to the same resource/method with a path parameter bound to “a”, “a/b” and “a/b/c”, respectively. So I create the follwoing @Path annotation:

@Path("foo/bar/{therest : .*}")

My initial source for this tidbit was the Safari Books Online copy of Restful Java with Jax-RS, page 47 which showed an example just like this.

When I tried to launch my web service, however, I got an error that indicated something was wrong with the regular expression.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Prototype': Error setting property values;
nested exception is org.springframework.beans.PropertyBatchUpdateException; 
nested PropertyAccessExceptions (1) are:PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'serviceBeans' threw exception;
nested exception is java.util.regex.PatternSyntaxException: Illegal repetition near index 27/foo/bar/{therest : .*}(/.*)?...

The answer was really simple, get rid of the spaces around the ‘:’. Argh! Since when do languages care that much about whitespace?! And Argh again! How can a published book have this error?! Or perhaps the bug is in CXF? I’ll look into that when I have a chance.

4 Comments

  1. Glen Mazza

    December 6, 2010
  2. Sergey Beryozkin

    December 6, 2010

    Hi CorneliaIt is likely a limitation (rather than a bug :-)) in CXF after all in that CXF does not strip spaces around the variables.This expression (without spaces) will work :@Path(“foo/bar/{therest:.*}”)The spaces around variables such as ‘therest ‘ can definitely be stripped – I’ll investigate further if the spaces around the actual regexpression can be stripped :” .*”The leading space may actually be needed, for example, if we have”{var: bar}” then this should match match “%20bar” in the decoded form.

  3. Sergey Beryozkin

    December 6, 2010

    Hi – this issue has just been fixed in 2.4.0-SNAPSHOT and 2.3.2-SNAPSHOT – thanks

  4. Cornelia Davis

    February 10, 2011

    Thanks Sergey.What did you end up figuring out on the space after the colon? I agree that there is certainly question on whether CXF can strip the space that follows the colon; as you said, perhaps that is intended as a part of the regex. That is why I whined about the bug being in the book to start.

Share Your Thoughts

Leave a Reply to Glen Mazza Cancel reply