users@jersey.java.net

Re: Intergrating Jetty and Jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 06 Mar 2008 14:32:48 +0100

Hi Jonathan,

Try the following:

   ServletHolder sh = new ServletHolder(ServletContainer.class);
   sh.setInitParameter(
     "com.sun.ws.rest.config.property.resourceConfigClass",
     "com.sun.ws.rest.api.core.PackagesResourceConfig");
   sh.setInitParameter(
     "com.sun.ws.rest.config.property.packages",
     <package name to search from>);

   Server server = new Server(9999);
   Context context = new Context(server, "/", Context.SESSIONS);
   context.addServlet(sh, "/*");
   server.start();

the above worked for me. At the end of the email is a complete stand
alone example. I used the following jars:

   jersey/lib/jersey.jar
   jersey/lib/asm-3.1.jar
   jersey/lib/jsr311-api.jar
   jetty-6.1.3/lib/jetty-6.1.3.jar
   jetty-6.1.3/lib/jetty-util-6.1.3.jar
   jetty-6.1.3/lib/servlet-api-2.5-6.1.3.jar


This is fairly cool, it means i now know how to use embedded Jetty for
unit testing the servlet container :-)

Hope this helps,
Paul.

package jetty;

import com.sun.ws.rest.api.client.Client;
import com.sun.ws.rest.api.client.WebResource;
import com.sun.ws.rest.spi.container.servlet.ServletContainer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

public class Main {

     @Path("/")
     public static class TestResource {

         @GET
         public String get() {
             return "GET";
         }
     }

     /**
      * @param args the command line arguments
      */
     public static void main(String[] args) throws Exception {
         ServletHolder sh = new ServletHolder(ServletContainer.class);
 
sh.setInitParameter("com.sun.ws.rest.config.property.resourceConfigClass",
                 "com.sun.ws.rest.api.core.PackagesResourceConfig");
         sh.setInitParameter("com.sun.ws.rest.config.property.packages",
"jetty");

         Server server = new Server(9999);
         Context context = new Context(server, "/", Context.SESSIONS);
         context.addServlet(sh, "/*");
         server.start();

         Client c = Client.create();
         WebResource r = c.resource("http://localhost:9999/");
         System.out.println(r.get(String.class));

         server.stop();
     }
}

Jonathan Cook - Online wrote:
> Does anybody know if it is possible to use Jetty and Jersey together
> rather than the built in HTTPHandler that comes with Jersey. I believe
> you can use Jersey with Tomcat so assumed you could use it with Jetty
> which is just another web server.
>
> Currently I have the following code:
> Server server = new Server(8080);
> Context context = new
> Context(server,"/",Context.SESSIONS);
>
> //config.
> com.sun.ws.rest.spi.container.servlet.ServletContainer container =
> new
> com.sun.ws.rest.spi.container.servlet.ServletContainer();
> container.init();
>
> context.addServlet(new ServletHolder(container), "/*");
>
> server.start();
>
> But when the Jetty server starts it complains about no paths found. I
> don't think it can find the resources successfully. Even if I pass some
> init params it still doesn't work.
>
> Do you know if Jersey will work with Jetty?
>
> Thanks
> Jonathan
>
>
>
>
> *_ _*
> *Jonathan Cook*
> Software Engineer
>
> Feeds Team
> *BBC Future Media & Technology (Journalism)
> *BC3 B1, Broadcast Centre
>
> *T:* 020 800 84734 (02 84734)
>
>
>
> http://www.bbc.co.uk
> This e-mail (and any attachments) is confidential and may contain
> personal views which are not the views of the BBC unless specifically
> stated.
> If you have received it in error, please delete it from your system.
> Do not use, copy or disclose the information in any way nor act in
> reliance on it and notify the sender immediately.
> Please note that the BBC monitors e-mails sent or received.
> Further communication will signify your consent to this.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109