1

I am implementing an API with Jersey 2.22.2, with the following exemplary GET URL:

http://example.com/api/users

Calling this URL returns, e.g., a list of users in JSON.

Now I want to be able to serve static content from the URL root, i.e.,

http://example.com/api/

I've included a simple index.html file in /webapps/, next to WEB-INF, and it gets built into the root of the WAR successfully.

However, when I navigate to http://example.com/api/ or http://example.com/api/index.html, I get a 404.

How can I configure web.xml or the servlet itself, to serve static content from the app root?

s.d
  • 4,017
  • 5
  • 35
  • 65
  • 1
    Why would you want the static content from `/api`? It's not part of the API. It should be served from `example.com/index.html`. That's the root. If `/api`, is your context path, it shouldn't be. Your context path should be nothing. `/api` should be the servlet mapping for the Jersey application. – Paul Samsotha Nov 27 '17 at 17:05
  • @peeskillet Thanks. Yes, context root is nothing. I wanted to serve API documentation at /api. Or is this something where you would usually return hypermedia with a link to an external folder like /docs? – s.d Nov 27 '17 at 17:35
  • You should put the docs in the `/api` folder in the `webapps`. Then you need to [configure Jersey to forward the request](https://stackoverflow.com/q/12422660/2587435). – Paul Samsotha Nov 27 '17 at 17:46
  • You _could_ just use a Jersey resource to serve it, but you want to take advantage of the caching done by the server's default servlet. Doing it this way make's Jersey forward the unknown requests to the default servlet. – Paul Samsotha Nov 27 '17 at 17:54

0 Answers0