• Post Reply Bookmark Topic Watch Topic
  • New Topic

Optional JAX-RS Parameters?

 
Mike London
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I make a parameter (@Path) optional in a JAX-RS service?

If I add a @Path for the parameter, then that path becomes part of the URL entered so I can get to the correct method.

For the "no parameter entered" case, would I create an overloaded method (without the parameter) or am I missing something more basic?

Basically, if the user enters the parameter, I want to use it, but if they don't enter the parameter, I want to use default behavior. What I have working so far is validating the parameter entered in the URL path expression, but not optionally not entering any path for the parameter.

Thanks,

-mike
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't actually tried this, but my gut feeling is that PathParams need to be present due to the way URL matching works. If there are several PathParams only the last one could be optional anyway (or a series of contiguous PathParams starting with the last), lest there be confusion about which part of the URL maps to which parameter.

If something is optional then a QueryParam would seem a natural choice, and that allows you to define default values.
 
Mike London
Ranch Hand
Posts: 1290
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I haven't actually tried this, but my gut feeling is that PathParams need to be present due to the way URL matching works. If there are several PathParams only the last one could be optional anyway (or a series of contiguous PathParams starting with the last), lest there be confusion about which part of the URL maps to which parameter.

If something is optional then a QueryParam would seem a natural choice, and that allows you to define default values.


Perfect answer. Exactly what I needed. Thanks.

-- mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!