title: REST and static content in jetty server
body: <p>I would like to have an (embedded) jetty+jersey server which provides the REST api (GET and POST on the "/resource" route) but also serves some static content (index.html, bundle.js on the "/"). I can only get the /resource part to work - GET/POST tested with curl i.e. </p>
  
  <pre><code>curl -X POST -H 'Content-Type: application/json' -d '{"resource":"FOOBAR"}' http://localhost:8080/resource
  </code></pre>
  
  <p>However the "/" path returns 404 with this setup:</p>
  
  <pre><code>public static void main(String[] args) {
  
          Server jettyServer = new Server(8080);
  
          ResourceConfig rc = new ResourceConfig();
  
          JSONResource resource = new JSONResource();
  
          rc.register(resource);
  
          ServletContainer sc = new ServletContainer(rc);
  
          ServletHolder servletHolder = new ServletHolder(sc);
          // ServletHolder servletHolder = new ServletHolder("default", new DefaultServlet());
  
          // static content
          servletHolder.setInitParameter("resourceBase", "./src/webapp/");
  
          ServletContextHandler context = new ServletContextHandler();
          context.addServlet(servletHolder, "/*");
  
          jettyServer.setHandler(context);
  
          try {
  
              jettyServer.start();
              jettyServer.join();
  
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              jettyServer.destroy();
          }
  
      }
  </code></pre>
  
  <p>Paths:</p>
  
  <pre><code>@Path("/resource")
  public class JSONResource {
  
      Resource resource =  Resource.getInstance();
  
      @GET
      @Produces(MediaType.APPLICATION_JSON)
      public String getStringResource() {
          return this.resource;
      }
  
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      public Response putStringResource(Resource json) {
  
          this.resource.setResource(json.getResource());
          return Response.status(Status.OK).entity(json).build();
      }
  
  }
  </code></pre>
  
  <p>And Resource class for completeness:</p>
  
  <pre><code>public class Resource {
  
      private String resource = "";
  
      private Resource() {
      }
  
      private static class Holder {
          private static final Resource INSTANCE = new Resource();
      }
  
      public static Resource getInstance() {
          return Holder.INSTANCE;
      }
  
      public String getResource() {
          return resource;
      }
  
      public void setResource(String resource) {
          this.resource = resource;
      }
  
      @Override
      public String toString() {
          return resource;
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>Edited per answer below by <a href="https://stackoverflow.com/users/3732793/user3732793">user3732793</a></p>
  
  <pre><code>    // ---JSON RESOURCE---//
  
      JSONResource resource = new JSONResource();
  
      ResourceConfig rc = new ResourceConfig();
      rc.register(resource);
  
      ServletContainer sc = new ServletContainer(rc);
  
      ServletHolder servletHolder = new ServletHolder(sc);
  
      ServletContextHandler jsonResourceContext = new ServletContextHandler();
      jsonResourceContext.addServlet(servletHolder, "/*");
  
      // ---STATIC RESOURCE---//
  
      ResourceHandler staticResourceHandler = new ResourceHandler();
  
      staticResourceHandler.setResourceBase("./src/webapp/");
  
      ContextHandler staticContextHandler = new ContextHandler("/");
      staticContextHandler.setHandler(staticResourceHandler);
  
      // ---ADD HANDLERS---//
  
      HandlerList handlers = new HandlerList();
  
      handlers.setHandlers(new Handler[] { jsonResourceContext, //
              staticContextHandler, //
              new DefaultHandler() //
      });
  
      jettyServer.setHandler(handlers);
  </code></pre>
  
  <p>Can it be done less verbose?</p>