users@jersey.java.net

Re: [Jersey] matching exact number of path segments

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 05 Aug 2009 09:43:53 +0200

On Aug 4, 2009, at 7:12 PM, Louis Polycarpou wrote:

> In order to process a pathparam that always contains a "/" (not
> encoded), I'm trying to match on an exact number of pathsegments,
> and have Jersey map this for me ideally to a String. Is there a
> regexp that will work for this - I tried a few things, but couldn't
> quite figure it out...
>
> Example:
>
> A field mediatype is always of the form "media/type"
>
> @GET("{media}/{type}")
> @Produces("text/plain")
> public Response getMedia(@PathParam ("{media}") String media,
> @PathParam ("{type}") String type ) {
> // now rebuild the String
> String mediatype = media + "/" + type;
>
> ...
> }
> The above will work but is there a way to do this with regexp in
> Jersey so that it parses the "/" into a single String for me?
>

By default the regular expression for a path parameter is [^/]+, i.e.
any but a /, which means a path segment.

You can express your own regular expression for a path parameter by
declaring the expression in the @Path:

   @Path("{segments: .+")

The above means one or more of any character, and thus consumes the
rest of the URI path that was not previously matched.

If you want to match something like you are doing above you can use
the following:

   @Path("{id: \\w+/\\w+}")

Paul.