Hi,

On 2015-01-18 13:03, Maarten Boekhold wrote:
I'm a bit confused still about the part where we tell the ServletHolder (or CFXNonSpringJaxrsServler?) to dispatch incoming requests to a specific object? The example I've seen of CXFNonSpringJaxrsServlet uses setInitParameter("javax.ws.rs.Application", "some.class.name"), but that implies that CXF manages the lifecycle of the application object, which won't work for me. I need to manage the lifecycle myself.

To demonstrate what I would like to do, I managed to implement this with Jersey & Jetty. However as mentioned previously, I'd really like to be able to do the same with CXF, because it just seems silly to have both CXF (which I need for non-JAX-RS-reasons as well) as well as Jersey in my project.

Code for Jersey+Jetty (note: works with Jersey 2.7, with 2.9 or 2.14 I'm getting errors which I haven't figured out yet):

   import org.eclipse.jetty.server.Server;
   import org.eclipse.jetty.servlet.ServletContextHandler;
   import org.eclipse.jetty.servlet.ServletHolder;
   import org.glassfish.jersey.server.ResourceConfig;
   import org.glassfish.jersey.servlet.ServletContainer;

   public class TheApp {
   TheResource resource;

        public TheApp(int port) throws Exception {
   ServletContextHandler sch = new ServletContextHandler();
   sch.setContextPath("/xxx");

   resource = new TheResource();
   ResourceConfig rc = new ResourceConfig();
   rc.register(resource);

   ServletContainer sc = new ServletContainer(rc);
   ServletHolder holder = new ServletHolder(sc);
   sch.addServlet(holder, "/*");

            Server server = new Server(port);
   server.setHandler(sch);
   server.start();
   server.join();
        }

        public static void main(String[] args) throws Exception {
            TheApp app = new TheApp(8122);
        }
   }

"TheResource" is simplicity itself:

   import javax.ws.rs.GET;
   import javax.ws.rs.Path;
   import javax.ws.rs.PathParam;
   import javax.ws.rs.Produces;
   import javax.ws.rs.core.MediaType;

   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;

   @Path("request")
   @Produces(MediaType.TEXT_PLAIN)
   public class TheResource {
        private static final Logger LOG =
   LoggerFactory.getLogger(TheResource.class);

        public TheResource() {
            LOG.info("Creating instance of TheResource");
        }

        @GET
        @Path("{service:.*}")
        @Produces("text/plain")
        public String testit(@PathParam("service") final String service) {
            LOG.info("GET method called with service = {}", service);
            return "Service = " + service;
        }
   }


Maarten

Reply via email to