Optional @PathParam in Jax-RS
Solution 1
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.
Solution 2
In my case I had to use this other expression:
@Path('/mypath/{param1 : (\\w+)?}')
Otherwise you have to clean the parameter.
Solution 3
Verify whether there is a path already defined with /mypath that accepts a different method, this could be the reason why you are getting 405 (Method not allowed) back. Also when you have optional parameters I guess it is better to make them query parameters.
Related videos on Youtube
Comments
-
Shane Larson almost 4 years
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?
-
Paul Samsotha over 8 years
-
-
Neeraj Jain over 8 yearsNot working in my case but this does
-
Zarathustra over 7 yearsIt is about
@Path
not@QueryParam
-
Shane Larson about 5 years@NeerajJain my question involves RestEasy, the one you linked is about Jersey.
-
QuirkyBit about 3 yearsThis was the error that I was getting. I had a
GET
at / and aPOST
at /{filename} where filename was optional. So the post at GET and POST were clashing. -
AlikElzin-kilaka about 2 yearsThe value of
param1
includes the slash (/
). Without manually cutting the slash in the code, is there a way to not include the slash in the value?