users@jsr311.java.net

Re: more comments to JSR311

From: Stephan Koops <Stephan.Koops_at_web.de>
Date: Tue, 25 Mar 2008 17:34:41 +0100

Hi Bill,

this

@GET
@Path("whatever") // no variable name (!)
public Object getWhatever(@Context PathSegment pathSegment) {
   ... // read matrix parameters or whatever
}

should be a shortcut for

@GET
@Path("whatever")
public Object getWhatever(@Context UriInfo uriInfo) {
   List<PathSegment> pathSegments = uriInfo.getPathSegments();
   PathSegment pathSegment = pathSegments.get(pathSegments.length()-1);
   ... // read matrix parameters or whatever
}

This will save two lines of code that has nothing to do with the web
resource logic and will raises the convience and with that the useability.
Here I can't use @PathParam(...), because there is no variable name.

But you are right, I forget that @Path could contain multiple path
segments. If you want to access the "customers" path segment of
"/customers/{id}" , you can't do it this way. But nevertheless it
produces better readable source code.

Stephan
>> *_at_Context on PathSegment?*
>> What about @Context also allowed on type PathSegment? I know that it
>> is also possible by using UriInfo.getPathSegments() and then fetch
>> the last, but this raises the useability. It should be the last
>> parsed path segment:
>>
>> * On a resource method the PathSegment that was parsed while looking
>> for the resource class (if it is a root resource class that from
>> @Path of the class)
>> * On a sub resource method or locator it is the one from @Path of
>> the java method.
> A @Path can be multiple path segments. So I don't see what you are
> saying here.
>
> I'm pretty sure you can do this:
>
> @Path("/customers/{id}"
> public String get(@PathParam("id") PathSegment id) {...}