Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] How to integrate ResourceHandler and Jersey-Servlet

Hello list,

what I wanted to create is an Java application (with standard static main method). In this application I create a Jetty server and want to combine it with Jersey ( JAX-RS). That works fine.

But now I want to add a ResourceHandler to server static JS, CSS and images from the webapp/ directory.

The Jersey integration I do like this:
--snip [1]--
ServletHolder holder = new ServletHolder(ServletContainer.class);
holder.setInitParameter(
    "com.sun.jersey.config.property.resourceConfigClass",
    "com.sun.jersey.api.core.PackagesResourceConfig"
);
holder.setInitParameter(
    "com.sun.jersey.config.property.packages",
    "de.weltraumschaf.maconha.resources"
);
holder.setInitParameter(
    "com.sun.jersey.api.json.POJOMappingFeature",
    "true"
);

Server                 server    = new Server();
SelectChannelConnector connector = new SelectChannelConnector();

connector.setPort(80);
server.addConnector(connector);

ServletContextHandler context = new ServletContextHandler(
    server,
    "/",
    ServletContextHandler.SESSIONS
);

context.addServlet(holder, "/*");
-- snap --

I read through some Jetty docu on eclipse.org and found infos about static file serving doing like this:
-- snip [2]--
ResourceHandler handler = new ResourceHandler();
handler.setDirectoriesListed(true);
handler.setWelcomeFiles(new String[]{ "index.html" });
handler.setResourceBase(".");
--snap--

I can get that working. But my questions are:

1. How do I configure the resourceBase() that way, that it access the webapp/ directory. And how do I do it without dependencies on the directory 'layout' so that it works either with maven-exec-plugin and also in the packaged jar. I've some experiences with loading resources by class loaders form src/main/resources. But how do I tell Jetty to use src/main/webapp relative (to the jar or source)?

2. How do I combine the ServletContextHandler with Jersey and the ResourceHandler that way that URIs like http://localhost/[css|img|js]/* invokes the ResourceHandler loading/serving the files from src/main/webapp/[css|img|js], but all other URIs (http://localhost/*) invokes the Jersey servlet?

I tried this (trial and error):
--snip--
Server                 server    = new Server();
SelectChannelConnector connector = new SelectChannelConnector();

connector.setPort(baseUri.getPort());
server.addConnector(connector);

HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {
        createJerseyServletHandler(server), // same as snip [1]
        createStaticResourceHandler(), // same as snip [2]
    new DefaultHandler()
});

server.setHandler(handlers);
--snap--

I tried to combine all what I've read on eclipse.org and the Jersy docu. But it does not really work as expected. It compiles and runs without excpetions

3. Last but not least: How do I configure Jetty in code that it uses the web.xml from the webapp dir. I found Infos about how to write the file but nothing about how to 'load' it into the server. I tried some examples to get mvn jetty:run working. But it does not

The full code of my project could be seen here https://github.com/Weltraumschaf/maconha/tree/master/client. Maybe the actual checkout will not compile because I'm just doing a lot of trial and error because I'm stuck in the documentation on eclispe.org.

I'm not that much experienced with Java, Servlets etc. I've just hacked an Jenkins-Plugin some weeks ago and the last time I used Java (1.3) 6 years ago.

Kind regards
-Sven


Back to the top