When you try to map a resource with a trailing slash using the standard @Path annotation, the methods get mapped to the same endpoint causing an exception.
Glassfish answers during the deployment with:
SEVERE: Following issues have been detected: WARNING: A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by @Consumes and @Produces annotations at Java methods ... These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.In Glassfish which uses the Jersey under the hood, the slash is also ignored. But there is a workaround. You can use regex in the Path-annotation to map resources ending with a slash. In the example at the end, I use regex to map the paths to the methods. But there is something you should know about the path-matching algorithm. The mapping algorithm uses the following rules:
The JAX-RS specification has defined strict sorting and precedence rules for matching URI expressions and is based on a most specific match wins algorithm. The JAX-RS provider gathers up the set of deployed URI expressions and sorts them based on the following logic:In the following example, test2 is found before test1 because the regex is longer! We check the trailing slash first. When it fails, it falls back to test1.
- The primary key of the sort is the number of literal characters in the full URI matching pattern. The sort is in descending order.
- The secondary key of the sort is the number of template expressions embedded within the pattern, i.e., {id} or {id : .+}. This sort is in descending order.
- The tertiary key of the sort is the number of nondefault template expressions. A default template expression is one that does not define a regular expression, i.e., {id}.
Jan,
BeantwoordenVerwijderenI would appreciate you adding your voice to https://java.net/jira/browse/JAX_RS_SPEC-463.