If you’ve worked with Jersey, you’re likely familiar with the embedded Grizzly server. Though I haven’t seen it mentioned often, Grizzly can indeed serve static content too. The following snippet is all that’s necessary to fire up an embedded web server. These examples were written with Grizzly 1.9.18-m and Jersey 1.4.
1 2 3 4
While I’ve found some
that you should be able to just add a Jersey
ServletAdapter to the
Grizzly server, that doesn’t appear to work with current versions.
Fortunately there are still a couple options that do work. If you want
to serve both your static pages and Jersey services from the same
context path, you can do something like the following. The key
differences from a regular Jersey adapter are to specify the location to
the static content in the
ServletAdapter constructor, and you need to
setHandleStaticResources to true.
1 2 3 4 5 6 7 8 9 10 11 12 13
If you want to have different context paths for your static content and services, you can create two adapters like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
One limitation to note is that this approach expects the files to be on the file system, which may not be what you’re looking for if you’re trying to create an executable jar type utility.