0

I am aware about the usage of PathParam annotation and the standard way to utilize it is :

@Path(/data/{id})
... getData(@PathParam("id") String id){...}

Can I use PathParam without a parameter in Path annotation? For eg:

@Path(/data)
... getData(@PathParam("id") String id){...}

If yes, what does the value of the string id depict?

Raj Dosi
  • 59
  • 7
  • 1
    It gives error in Spring Boot - `MissingPathVariableException: Missing URI template variable 'id' for method parameter of type String`. `@Path` and `@PathParam` are of JAX-RS, not Spring, but the behaviour should be the same I think. Why don't you try running it instead of posting a question? – Kartik Nov 02 '18 at 05:29
  • I got a code snippet which is functional and is of the same pattern mentioned in the question. Hence the question. Must be that the value is being carried forward from parent class. – Raj Dosi Nov 02 '18 at 05:58

1 Answers1

1

I think what you need to achieve is have an optional path parameter named id. You can achieve that using the following path param with regex:

@Path(/data/{id : (.+)?})
... getData(@PathParam("id") String id){...}

This way, id will be null if no path param is provided. Otherwise the provided value will be given.

Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
  • okay. So, I believe implementing pathParam implies having a parameter in Path annotation, which could be made optional as you suggested. If the value is missing, it will throw an exception suggested in the comment by @Kartik. – Raj Dosi Nov 02 '18 at 05:55
  • 1
    If you used in the way suggested by @Kartik, then it will throw an exception. But with my approach, it will only be `null`. So, if the value is missing, you will get a `null` for `id`. You will need to check for null within the method. – Imesha Sudasingha Nov 02 '18 at 05:59