LeonardBlunderbuss
LeonardBlunderbuss

Reputation: 1274

Can't access static resources from Jetty ResourceBase files when using a Jersey ServletHolder

I'm working on porting an existing war project to embedded Jetty. I've tried about 5 different ways of setting up the server and below is the only method that has worked for me:

public class WebApp {

  public static void main(String[] args) throws Exception { 
    Server server = new Server(8080);

    ResourceConfig resourceConfig = new ResourceConfig();       
    resourceConfig.packages(HelloWorldResource.class.getPackage().getName(),GoodbyeWorldResource.class.getPackage().getName());
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    ServletHolder sh = new ServletHolder(servletContainer);  

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase("../src/main/webapp");
    context.addServlet(sh, "/*");
    server.setHandler(context);

    server.start();
    server.join();
  }
}

The only problem is that setResourceBase doesn't seem to work; I can't access any html files in my webapp directory (although I can read them in as Files within my code using the same relative path). I'm surely missing something simple here.

Upvotes: 0

Views: 775

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

First of all, don't use relative paths for the Base Resource, use fully qualified paths instead.

For 2 examples on this technique, see the embedded-jetty-cookbook project:

With that out of the way, the problem you are having seems to be that you are using Jersey and assuming that Jetty is serving the static resources for you.

When using Jersey, Jersey itself will serve the static resources, and never lets Jetty actually serve those static files.

Upvotes: 2

Related Questions