45

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?

Fábio
  • 3,291
  • 5
  • 36
  • 49

4 Answers4

47

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Fábio
  • 3,291
  • 5
  • 36
  • 49
10

In my case I had to use this other expression:

@Path('/mypath/{param1 : (\\w+)?}')

Otherwise you have to clean the parameter.

aruizca
  • 1,891
  • 1
  • 19
  • 13
2

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.

Prasanna
  • 3,703
  • 9
  • 46
  • 74
  • This was the error that I was getting. I had a `GET` at / and a `POST` at /{filename} where filename was optional. So the post at GET and POST were clashing. – QuirkyBit Feb 27 '21 at 01:56
-11

With Dropwizard 1.0 and Java8 you can use Optional

@Path("/news")
getLastNews(@QueryParam("topicId") String topicId, @QueryParam("limit") Optional<Integer> limit) 

It will give a response to both

/news?topicId=123213?limit=200

and

/news?topicId=123213
Olesia
  • 144
  • 4