users@jersey.java.net

Re: [Jersey] How do I get a given path to call the right method

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Thu, 26 Jun 2008 15:00:58 -0400

On Jun 26, 2008, at 2:30 PM, Kevin Duffey wrote:
>
> So I want basically
>
> DELETE /myresource/resource-name
>
Here's one way you could do it:

@Path("myresource")
public class MyResource {

   @DELETE @Path("{name}")
   public void deleteIt(@PathParam("name") String name) {
     ...
   }
}

Then when you issue a DELETE /myresource/resource-name it will call
the deleteIt method and the name parameter will have the value
"resource-name".

> Furthermore, we are going to have a situation very soon where we
> have a something like /myresource/res-id/instances/instance-id, and
> we'll want to drill down to the instance-id and send in calls. I
> have yet to find how to do this with Jersey. Do I specify a second
> class, say InstancesResource, and put an @Path("/myresources/{res-
> id}/instances") or is there some way that the /myresource class
> handles/parses the remaining path info and calls appropriate methods?
>
There are a couple of ways you could do this. The first is similar to
the example above but with a longer @Path. The second is to use a
subresource locator like this:

@Path("myresource")
public class MyResource {

   @Path("{res-id}/instances/{inst-id}")
   public InstanceResource findInstance(@PathParam("res-id") String
resId,
       @PathParam("inst-id") String instId) {
     InstanceResource instanceResource = new InstanceResource(resId,
instId);
     return instanceResource;
   }
}

where InstanceResource is a resource class that has method with @GET,
@DELETE etc. Any request, regardless of method, that matches the URI
pattern will be directed to the findInstance method and the jersey
runtime will then use the returned instance to handle the request.

Hope that helps,
Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.