Jersey resource that matches any path


I am using <a href="https://jersey.java.net/documentation/1.19/index.html" rel="nofollow">Jersey v1.x</a> and a <a href="https://github.com/google/guice/wiki/ServletModule" rel="nofollow">Guice Servlet</a>. What I'm trying to do is bind a Jersey Resource that matches <em>any</em> @Path, such that I can use Jersey to respond with a 404.

I'm looking to do this, since my servlet consists of different components (e.g. a rest API that lives under /api, and a web UI that lives under /. In Guice terms, that means I have several ServletModules that each set up one part of the servlet:

<ul><li>In my ApiServletModule: serve("/api").with(GuiceContainer.class, conf)</li> <li>In my WebUiServletModule: serve("/").with(GuiceContainer.class, conf)</li> </ul>

In this setup, I want to define what the 404 response body looks like for each part of the webapp (/api or /) from the codebase of each subproject responsible, without having to reimplement Jersey

<hr />

So far I have tried to bind a resource that match @Path("/"), @Path("*") and @Path("/*"), but none of these seem to be picked up when I request /some/path/that/doesnt/exist


You need to use the regex format of the path expression, i.e.

@Path("{any: .*}")

You could inject List<PathSegment> to look at all the segments if you need them.

public Response getSomething(@PathParam("any") List<PathSegment> segments)


<a href="https://stackoverflow.com/users/2587435/" rel="nofollow">@peeskillet</a>'s <a href="https://stackoverflow.com/a/30834819/1296709" rel="nofollow">answer</a> is indeed correct, in the sense that it describes how you can create a Jersey resource that matches any path.<br /> However, my goal of creating a resource that delivers 404 responses for whenever any other unmatched path is requested is not quite met by this answer:

At least in combination with Guice, will such a "match all"-resource intercept all requests, regardless of whether any more specific resources are available. Additionally, you cannot modify the HTTP response status code from within a resource.

For this purpose, Jersey has ExceptionMappers that can be implemented and loaded by adding the @Provider annotation. One particular type would be a ExceptionMapper<NotFoundException>, which is invoked when a Resource throws a NotFoundException. The ExceptionMapper can then decide what response to generate, including the status code.


  • How to hook Jackson ObjectMapper with Guice / Jersey
  • how to enhance ebean models in a jar
  • Including/excluding entire groups from targets
  • Android Video Editor API
  • Play Framework 2.2.1 - Adding non-play Java Project as sub-project
  • Google app engine task queue and users service
  • ContextNotActiveException in simple Faces Flow with CDI
  • AWS Elastic Beanstalk - changing owner of webapp folder
  • Grunt module(s) to upload all static assets to S3 / CloudFront, replace paths, and invalidate old as
  • Trouble connecting to Google Cloud SQL server from deployed app
  • How to change default stop edit behavior in jtable
  • How to use SBT with multiple sub project web applications?
  • call a java program from a webapp in tomcat server - the java program is out side of tomcat server
  • Arduino making decision according to a packet received from serial port
  • Can't compile Arduino CapSense example
  • Mercurial: Identify file name after rename
  • Vigenere cipher not working
  • matlab crashes without dump file when using fopen for file
  • Prevent page break in text block with iText, XMLWorker
  • File extension of zlib zipped html page?
  • Python 3.2.2, error(scripts to exe)
  • NSIS decompiler
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Problem while Building a Setup Project for a windows Service?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Play WS (2.2.1): post/put large request
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • What is the “return” in scheme?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • Unable to use reactive element in my shiny app