1

Question(s):

  • How can I define Jersey resources and ExceptionMappers in such a way that they only respond to requests for a certain subset of paths?
  • How can I do this without having to decide ahead of time under what base path I would like to host them later?
  • How can do this such that I can create a single war from composing libraries that each define resources for a particular base path of the web app?

Background:
I have a Guice + Jersey v1 webapp, that loads two ServletModules from two different dependencies (jars).
One of them defines resources for rest API endpoints (e.g. @Path("/purchases"), @Path("/payment-methods")), the other defines resources for a web UI (e.g. @Path("/"), @Path("/products")).
The ServletModule for the former attempts to tie its Jersey resources to the "/api/" base path (serve("/api/*").with(GuiceContainer.class, conf);), while the latter does the equivalent for the "/" basepath (serve("/*").with(GuiceContainer.class, conf);)
Both dependencies also define their own Jersey ExceptionMappers; The API's ExceptionMappers respond with a JSON error object, while the web UI's ExceptionMappers generate a HTML response with the help of Freemarker.

However, the result of this combination - where both the API and web UI jars are on the classpath - is that Jersey resources from both dependencies respond to requests that have a path beginning with "/api/" and "/".
E.g. the API resource for /purchases responds to requests for http://example.com/api/purchases and http://example.com/purchases. Similarly, the web UI resource for /products responds to requests for http://example.com/products and http://example.com/api/products.

The fact that Jersey sees two ExceptionMappers for the same exception type (e.g. NotFoundException) makes it pick only one, regardless of what the requested path starts with. In my case, the ExceptionMapper<NotFoundException> responds to unknown resource requests under both /api/ and /, which is bad for either part of the app.

derabbink
  • 2,419
  • 1
  • 22
  • 47
  • Not sure I _completely_ understand the whole situation, but maybe [this will help](http://stackoverflow.com/q/7581393/2587435) – Paul Samsotha Jun 19 '15 at 02:06
  • Thanks. [That](http://stackoverflow.com/a/16978646/1296709) will take care of the `ExceptionMapper` issue. It requires that my main/outer `ExceptionMapper` implements the `"/api/"` and `"/"` base-path split as well, but it's good enough :) – derabbink Jun 19 '15 at 08:13
  • What I would still like to know is how I can prevent a Jersey resource from responding to requests under the `"/"` basepath, when it is bound in a `ServletModule` that's supposed to only configure resources under the `"/api/"` basepath. In the example in the question, the `@Path("/purchases")` resource should only respond to `/api/purchases` requests, but it also responds to `/purchases` - simply because I have another servlet module that binds more resources and serves them under `"/"`. Let me know if I need to add a more complete code snippet that paints the whole picture. – derabbink Jun 19 '15 at 08:13

0 Answers0