Optional @PathParam in Jax-RS

Fábio picture Fábio · Mar 24, 2011 · Viewed 41.9k times · Source

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?

Answer

Fábio picture Fábio · Mar 29, 2011

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.