Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional @PathParam in Jax-RS

I have a service where the last part of the path is optional, the user can both enter /mypath/ and /mypath/param1/.

I tried to use a regular expression to filter the last part of the path:

@Path("/mypath{param1: (/param1)?}")

I'm using RestEasy as my JAX-RS provider and the code works as expected in Tomcat but when I deploy it in JBoss I get a 405 return code when I do not submit the optional part.

Am I doing something wrong here or it's not possible to accomplish this in a portable way?

like image 985
Fábio Avatar asked Mar 24 '11 14:03

Fábio


People also ask

Can path params be optional?

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

What does annotation @PathParam for resource mapping?

The @PathParam annotation is a type of parameter that you can extract for use in your resource class. URI path parameters are extracted from the request URI, and the parameter names correspond to the URI path template variable names specified in the @Path class-level annotation.

Is Path Param mandatory?

Path parameters are part of the endpoint and are required. For example, `/users/{id}`, `{id}` is the path parameter of the endpoint `/users`- it is pointing to a specific user's record. An endpoint can have multiple path parameters, like in the example `/organizations/{orgId}/members/{memberId}`.

What is PathParam in rest?

@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.


1 Answers

The problem was the lack of whitespace before the colon:

@Path("/mypath{param1: (/param1)?}") 

should be:

@Path("/mypath{param1 : (/param1)?}") 

Apparently it's a bug, because the specification makes the whitespace around the colon optional. I also found that I'm not the first bitten by this bug.

like image 130
Fábio Avatar answered Sep 22 '22 03:09

Fábio