users@jersey.java.net

Re: [Jersey] _at_PathParam doesn't inject in property field

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 26 Aug 2009 16:45:32 +0200

On Aug 26, 2009, at 4:23 PM, sdellafiore wrote:

> Hi all, I'm an happy user of jersey 1.1, but I'm having some
> problems using the @PathParam annotation. According to the
> @PathParam annotation, it should work with parameters, fields and
> methods, but I'm able to make it work with parameters only. I give 2
> code fragments as an example: 1) @Path("channel")
> @Produces("application/javascript") @PerRequest public class
> ChannelResource { @GET @Path("/{channel}/popular/items") public
> JSONWithPadding getPopularItems( @DefaultValue("ever")
> @QueryParam("timespan") String timespan, @PathParam("channel")
> String channel) { ... } } 2) @Path("channel") @Produces("application/
> javascript") @PerRequest public class ChannelResource
> { @PathParam("channel") private String channel; @GET @Path("/
> {channel}/popular/items") public JSONWithPadding
> getPopularItems( @DefaultValue("ever") @QueryParam("timespan")
> String timespan) { ... } } When I call http:////channel/local/
> popular/items the code in 1) works properly, and the "channel"
> method parameter is given the value "local". The code in 2) doesn't
> extract channel value (the channel property stays is set to null).
> Am I doing something wrong?

Yes, when class ChannelResource is instantiated the algorithm does not
not know anything about the sub-resource method getPopularItems
annotated with @Path("/{channel}/popular/items"), so only path
parameters declared in the @Path of the class are accessible.

Note that there could be many different sub-resource methods each with
different @Path declarations containing different path parameter names.

For fields to work you would need to do something like the following:

    @Path("channel/{channel}/")
    @Produces("application/javascript")
    @PerRequest
    public class ChannelResource {

      @PathParam("channel") String channel;

      @GET @Path("popular/items")
      public JSONWithPadding getPopularItems(
        @DefaultValue("ever") @QueryParam("timespan") String timespan)
{ ... }


Paul.


> View this message in context: @PathParam doesn't inject in property
> field
> Sent from the Jersey mailing list archive at Nabble.com.