users@jersey.java.net

[Jersey] Re: _at_PathParam optional components.

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Mon, 31 Oct 2011 16:33:00 +0100

Hi Praveen,

you may consider the following:

@Path("/service/animal:{animal}")
public class AnimalResource {

   @PUT // well you did not specify the HTTP method, so i picked PUT
   public void doPutTypeNotSpecified(@PathParam("animal") String animal) {
      doPutWithType(animal, null);
  }

   @PUT
   @Path("type:{type}")
   public void doPutWithType(@PathParam("animal") String animal,
@PathParam("type") String type) {
       // do whatever you need
   }
}

Another option would be to use:

@Path("/service/animal:{animal}/{type: (type:.*)?}")

but that means the type parameter always includes the "type:" prefix
if there is any such type present.

Another option then would be to go with:

@Path(/service/animal:{animal}/{type: .*}

where type parameter will simply contain the rest of the URI after the
animal parameter.
You still need to parse manually.

~Jakub

On 23.10.2011 7:00, Praveen Cherukuri wrote:
> Hello All,
>
> I have a method annotated with something like
>
> @Path("/service/animal:{animal}/type:{type}")
> public void doStuff(@PathParam("animal") String animal,
> @PathParam("type") String type)) {
> .....
> }
>
> The above seems to work. Now, I would like to make "type:{type}" part
> of the URI optional. So I was trying to do something like below. It's
> ok if the method variable is assigned null.
>
> @Path("/service/animal:{animal}/(type:{type})?")
> public void doStuff(@PathParam("animal") String animal,
> @PathParam("type") String type)) {
> .....
> }
>
> But this doesn't seem to work. Any ideas on how this can be
> accomplished? I think MatrixParameters are an option. But I don't want
> to pursue that if I don't have to.
>
> Thanks in advance for your response.
>
> Regards,
> Praveen.