users@jersey.java.net

Re: [Jersey] Default values for path params?

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Fri, 18 Dec 2009 16:50:36 -0500

On Dec 18, 2009, at 3:44 PM, Comerford, Sean wrote:

> Query parameters support the @DefaultValue annotation.
>
> Is there any equivalent for PathParams if I want to support leaving one off and assuming a default?
>
> My use case here is that my resource exposes basketball player season stats, i.e. /stats/player/season/{playerId},{season}
>
> But I want to be able to allow callers to leave off the season part if they just want the current season.
>
> I guess my only option is to make season a query param instead of path param?
>
You can specify the regex for a path parameter. By default you get [ˆ/]+? which matches one or more chars up to the next '/'. Instead you could use something like /stats/player/season/{playerId}{season: ,[^/]*?} (you may need to tweak that, my regex skills have gotten rusty) so the @Path will still match without the trailing ",season". Then the @PathParam{season} will be empty if none is specified in the URI.

Another alternative is to use a matrix parameter:

e.g. URI: /stats/player/season/playerX;season=2009

@Path("/stats/player/season/{playerId}")
@PathParam("playerId") = "playerX"
@MatrixParam("season") = "2009"

> Furthermore, and this is probably a dumb question but I’m slow on the upteake, in terms of proper RESTful design, is there a good rule of thumb on using a path param vs query param?
>
The RESTful Web Service book by Richardson and Ruby suggests using path segment to designate a path through a directed graph or elements of a hierarchy. It suggests using query parameters to designate arguments being plugged into an algorithm.

Marc.