user3062513
user3062513

Reputation: 410

Regarding optional path in JAX-RS

I want to pass an optional parameter in the JAX-RS path.I am using the below path but its not working.

@Path("/lock/{userName}/{userid:(([a-zA-Z]{2})?)}")

The resource should get invoked for both with userid and without user id parameter in the path. Can anyone suggest me what needs to be done?

Thanks

Upvotes: 3

Views: 1821

Answers (2)

bogwin19
bogwin19

Reputation: 1

It's also possible to use FormParam instead of PathParam

@POST
@Path("/lock/{userName}")
public Response resourceName(@PathParam("userName") String userName, @FormParam("userId") String userId)

You can provide the userId or not.

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209092

You could take out the / between the two template parameters and insert it into the regex of the userId

@Path("/lock/{userName}{userid:((/[a-zA-Z]{2})?)}")

Won't make a difference, but the extra wrapping parenthesis are not needed,
i.e. this {userid: (/[a-zA-Z]{2})?} is sufficient

Upvotes: 2

Related Questions