dReAmEr
dReAmEr

Reputation: 7196

Optional parameter in @PathParam annotation

We are facing issue related with making a path parameter optional.

original URL /expire/{token}

What we are trying to do is have the same service work for the URL's below.

   1. /expire/{token}
   2. /expire

Ex:- @Path("/expire/{token}")

We have already applied other solutions from SO,but no luck so far.

Upvotes: 7

Views: 27838

Answers (3)

Rajaneesh
Rajaneesh

Reputation: 181

We can use regular expressions,

"@Path("/add/{a:([0-9.]*)}/{b:([0-9.]*)}{p:/?}{c:([0-9.]*)}")"

here path can be

add/2/3
add/2/3/4 

my case I am using regular expressions for allowing only numbers

Upvotes: 1

Karthik R
Karthik R

Reputation: 5786

Logically, it doesn't seem to make sense to have it optional. Your URI should handle the type of request it's supposed to do. But, I came across a post to make the @PathParam to be optional with a small hack using regular expressions.

http://www.nakov.com/blog/2009/07/15/jax-rs-path-pathparam-and-optional-parameters/

I would go with having separate endpoint method in Controller where it can pass the call to your services with optional parameter.

Upvotes: 5

Peter Salomonsen
Peter Salomonsen

Reputation: 5683

What about adding another method annotated with only:

@Path("/expire")

And let this method pass a null value into the original method.

Upvotes: 18

Related Questions