users@jersey.java.net

Re: [Jersey] Representation addressability and the _at_Path annotation

From: Alex Sherwin <alex.sherwin_at_acadiasoft.com>
Date: Mon, 23 Mar 2009 16:50:22 -0400

I might be missing something in your question, but I just tried this and
it works for me...

  @GET
  @Path("/img/{imgName: [a-zA-Z0-9_]+\\.png}")
  public Response getPng(@PathParam("imgName") final String imgName) {
    LOG.info("Got imgName request: " + imgName);
    return Response.ok().entity("Got image request for imgName: " +
imgName + "!").build();
  }

Browser request prints:
"Got image request for imgName: imgName.png!"



Casper Bang wrote:
> For addressability and debugging reasons, I'd like to supply an
> alternative representation resolution than simply relying on MIME
> header data and @Produces annotations. The handy @Path annotations
> opens up for some nice reuse at method level:
>
> @Path("/meters/{meterId: " + meterId + "}")
> public class{
> @GET
> @Path("png")
> @Produces("image/png")
> public byte[] getAsImage{ ... }
> }
>
> This would give me the resource as a PNG image if I issue a GET
> /meters/755841/png
>
> Now, what I would really like is to modify the adressability aspect a
> bit to mimic people's common understanding of the web, such that I can
> issue a GET /meters/755841.png (and ...755841.json etc.) but that does
> not seem possible given how the trailing / is mandatory and needed for
> Jersey to be able to parse/tokenize. The obvious workaround I can see
> is to include the variable (meterId) in each and every method level
> @Path annotation:
>
> @Path("/meters/)
> public class{
>
> @GET
> @Path("{meterId: "+ meterId +"}.png")
> @Produces("image/png")
> public byte[] getAsImage{ ... }
>
> @GET
> @Path("{meterId: "+ meterId +"}.json")
> @Produces("application/json")
> public byte[] getAsJson{ ... }
>
> // ...etc.
> }
>
> However this does not seem very DRY and potentially not the best use
> of Jersey. So is this the only way? Or can I somehow tell Jersey to
> tokenize on dot as well as slash?
>
> Thanks in advance,
> Casper
>
>
> PS: By the way, the above @Path expression won't actually compile. I
> get the error "attribute value must be constant" when using "{meterId:
> "+ meterId +"}.png". The way around that seems to be to concatanate
> into a public static final String member of the resource and use it
> instead. Not sure I understand why concatanation in a @Path value is
> allowed at class level but not at method level?