Categories
discuss

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} …

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.

Answer

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.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..