users@jersey.java.net

RE: Intergrating Jetty and Jersey

From: Jonathan Cook - Online <Jonathan.Cook2_at_bbc.co.uk>
Date: Thu, 6 Mar 2008 17:33:25 -0000

Unforunately. Using cglib-nodep2.1.3 and removing the hibernate asm jars
doesn't seem to work although I do get a different error so maybe it is
a step in the right direction. I end up with
org.hibernate.HibernateException: createQuery is not valid without
active transaction
        at
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWra
pper.invoke(ThreadLocalSessionContext.java:297)
        at $Proxy23.createQuery(Unknown Source)
        at
com.bbc.newsi.feeds.sport.presentation.footballv3.Football.getFixtures(F
ootball.java:135)
        at
com.bbc.newsi.feeds.sport.presentation.footballv3.Football.getFixturesFo
rCompetition(Football.java:124)
        at
com.bbc.newsi.feeds.sport.presentation.football.resources.HelloWorldReso
urce.getFootballFixtures(HelloWorldResource.java:39)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at
com.sun.ws.rest.impl.model.method.dispatch.EntityParamDispatchProvider$T
ypeOutInvoker._dispatch(EntityParamDispatchProvider.java:107)
        at
com.sun.ws.rest.impl.model.method.dispatch.ResourceJavaMethodDispatcher.
dispatch(ResourceJavaMethodDispatcher.java:66)
        at
com.sun.ws.rest.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java
:104)
        at
com.sun.ws.rest.impl.uri.rules.ResourceClassRule.accept(ResourceClassRul
e.java:55)
        at
com.sun.ws.rest.impl.uri.rules.RightHandPathRule.accept(RightHandPathRul
e.java:83)
        at
com.sun.ws.rest.impl.uri.rules.RootResourceClassesRule.accept(RootResour
ceClassesRule.java:49)
        at
com.sun.ws.rest.impl.application.WebApplicationImpl.handleRequest(WebApp
licationImpl.java:246)
        at
com.sun.ws.rest.spi.container.servlet.ServletContainer.service(ServletCo
ntainer.java:123)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

I'm a bit stuck now, I thought maybe I could use a newer version of
cglib 2.2 but its only beta and doesn't solve the problem.

I also tried that javassist tip but that didn't help either and we're
not using Spring so the Spring configuration doesn't help either.

Any idea..
-----Original Message-----
From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
Sent: 06 March 2008 13:33
To: users_at_jersey.dev.java.net; Jonathan Cook - Online
Subject: Re: Intergrating Jetty and Jersey

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
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.