users@jersey.java.net

Representation addressability and the _at_Path annotation

From: Casper Bang <casper_at_jbr.dk>
Date: Mon, 23 Mar 2009 21:37:44 +0100

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?