Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parameter in @PathParam annotation

Tags:

java

rest

jersey

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.

like image 796
dReAmEr Avatar asked Sep 07 '15 09:09

dReAmEr


People also ask

Can path Param be optional?

Required and Optional Parameters Note that path parameters must have required: true , because they are always required.

What is optional parameter in REST API?

You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.

How do I make optional arguments in spring boot?

Using an Optional Parameter Type. Here, Spring creates the Optional<Integer> instance, optionalArticleId, to hold the value of id. If id is present, optionalArticleId will wrap its value, otherwise, optionalArticleId will wrap a null value.

How do you add optional parameters in Java?

There are no optional parameters in Java. What you can do is overloading the functions and then passing default values.


2 Answers

What about adding another method annotated with only:

@Path("/expire")

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

like image 163
Peter Salomonsen Avatar answered Sep 19 '22 10:09

Peter Salomonsen


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.

like image 45
Karthik R Avatar answered Sep 17 '22 10:09

Karthik R