Okay forgot the init params code now looks like this:

        ServletAdapter adapter = new ServletAdapter();
        Injector injector = Guice.createInjector(new MyModule());

        GuiceContainer container = new GuiceContainer(injector);
        adapter.setServletInstance(container);
        GuiceFilter filter = new GuiceFilter();
        adapter.addFilter(filter, "guiceFilter", null);
        adapter.addInitParameter("com.sun.jersey.config.property.packages",
                "com.netdesign.rest");
        SelectorThread threadSelector =
GrizzlyServerFactory.create(BASE_URI,
                adapter);

But I get a null pointer where the injected property should have been
called:


SEVERE: service exception:
java.lang.NullPointerException
    at com.netdesign.rest.MyResource.getIt(MyResource.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:156)
    at
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
    at
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:208)
    at
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:75)
    at
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:115)
    at
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:67)
    at
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:775)
    at
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:740)
    at
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:731)
    at
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:372)
    at
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:452)
    at
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:633)

And a little code :

@Path("/myresource")
public class MyResource {

    private MessageDao myTest;
    // TODO: update the class to suit your needs

    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getIt() {
        return myTest.getMessage();
    }
    @Inject
    public void setMyTest(MessageDao myTest) {
        this.myTest = myTest;
    }


}

and the module:

    @Override
    protected void configure() {
        bind(MessageDao.class).to(MyTest.class);

    }


2010/3/5 nino martinez wael <[email protected]>

> Theres something wrong, worked fine with no guice bindings, but as soon as
> I got one I get this:
>
> Starting grizzly...
> 2010-03-05 09:27:08
> com.sun.jersey.server.impl.application.WebApplicationImpl initiate
> INFO: Initiating Jersey application, version 'Jersey: 1.1.4.1 11/24/2009
> 01:30 AM'
> 2010-03-05 09:27:08
> com.sun.jersey.server.impl.application.WebApplicationImpl
> processRootResources
> SEVERE: The ResourceConfig instance does not contain any root resource
> classes.
> 2010-03-05 09:27:08 com.sun.grizzly.http.servlet.ServletAdapter service
> SEVERE: service exception:
> com.sun.jersey.api.container.ContainerException: The ResourceConfig
> instance does not contain any root resource classes.
>     at
> com.sun.jersey.server.impl.application.WebApplicationImpl.processRootResources(WebApplicationImpl.java:849)
>     at
> com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:718)
>     at
> com.sun.jersey.guice.spi.container.servlet.GuiceContainer.initiate(GuiceContainer.java:112)
>     at
> com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:253)
>     at
> com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:521)
>     at
> com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:199)
>     at
> com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:308)
>     at
> com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:471)
>     at javax.servlet.GenericServlet.init(GenericServlet.java:241)
>     at
> com.sun.grizzly.http.servlet.ServletAdapter.loadServlet(ServletAdapter.java:327)
>     at
> com.sun.grizzly.http.servlet.ServletAdapter.service(ServletAdapter.java:268)
>     at
> com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:165)
>     at
> com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:659)
>     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:577)
>     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:829)
>     at
> com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:162)
>     at
> com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:136)
>     at
> com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:103)
>     at
> com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:89)
>     at
> com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
>     at
> com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:67)
>     at
> com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
>     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:269)
>     at java.util.concurrent.FutureTask.run(FutureTask.java:123)
>     at
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
>     at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
>     at java.lang.Thread.run(Thread.java:595)
>
> And the code:
>
>
>     protected static SelectorThread startServer() throws IOException {
>         final Map<String, String> initParams = new HashMap<String,
> String>();
>
>         initParams.put("com.sun.jersey.config.property.packages",
>                 "com.netdesign.rest");
>
>         System.out.println("Starting grizzly...");
>
>         ServletAdapter adapter = new ServletAdapter();
>         Injector injector = Guice.createInjector(new MyModule());
>
>
>         GuiceContainer container = new GuiceContainer(injector);
>         adapter.setServletInstance(container);
>         GuiceFilter filter = new GuiceFilter();
>         adapter.addFilter(filter, "guiceFilter", null);
>
>         SelectorThread threadSelector = GrizzlyServerFactory.create(
>                 BASE_URI, adapter);
>
>
>         return threadSelector;
>     }
>
>     public static void main(String[] args) throws IOException {
>         SelectorThread threadSelector = startServer();
>         System.out.println(String.format(
>                 "Jersey app started with WADL available at "
>                         + "%sapplication.wadl\nHit enter to stop it...",
>                 BASE_URI));
>         System.in.read();
>         threadSelector.stopEndpoint();
>     }
>
>
> 2010/3/5 nino martinez wael <[email protected]>
>
> Hi Eelco
>>
>> Thanks for the response :)
>>
>> Distilling it, it becomes something like this:
>>
>>         ServletAdapter adapter = new ServletAdapter();
>>         Injector injector =Guice.createInjector(MyModule);
>>         GuiceContainer container=new GuiceContainer(injector);
>>
>>         adapter.setServletInstance(container);
>>         GuiceFilter filter = new GuiceFilter();
>>         adapter.addFilter(filter, "guiceFilter", null);
>>
>>         SelectorThread threadSelector = GrizzlyServerFactory.create(
>>                         "http://localhost:9998/";, adapter);
>>
>> Right?
>>
>> \/\/
>> -Nino
>>
>> 2010/3/4 Eelco Hillenius <[email protected]>
>>
>> > I'm using a tweaked version of contribs/jersey-guice, part of the
>>> > jersey project. See
>>> >
>>> https://jersey.dev.java.net/nonav/apidocs/1.1.5/contribs/jersey-guice/index.html
>>>
>>> Here is the an example of code that I use to setup JAXRS/ Jersey with
>>> Guice, but you have to read through the fluff a bit since we built a
>>> mini framework around Guice.
>>>
>>>                Settings settings = new Settings();
>>>
>>>  settings.setSetting("com.sun.jersey.config.property.packages",
>>>
>>>  ErrorTrapTest.class.getPackage().getName());
>>>
>>>                Dependencies dependencies = new Dependencies(settings,
>>>                                new ServletModule(), new
>>> LocalServiceRegistryModule(),
>>>                                testModule, new SchedulerModule(settings),
>>>                                new ErrorTrapModule());
>>>
>>>                ServletAdapter adapter = new ServletAdapter();
>>>                JerseyGuiceContainer container = dependencies
>>>                                .getInstance(JerseyGuiceContainer.class);
>>>                adapter.setServletInstance(container);
>>>                GuiceFilter filter = new GuiceFilter();
>>>                adapter.addFilter(filter, "guiceFilter", null);
>>>
>>>                SelectorThread threadSelector =
>>> GrizzlyServerFactory.create(
>>>                                "http://localhost:9998/";, adapter);
>>>
>>>
>>> In the code above, Settings is a boosted properties loader and
>>> Dependencies a wrapper around Injector (which is created in the
>>> constructor) that amongst other things binds settings to Names
>>> (Names.bindProperties(binder(), settings.getProperties());).
>>> JerseyGuiceContainer is a slightly tweaked version of GuiceContainer
>>> (
>>> https://jersey.dev.java.net/nonav/apidocs/1.1.5/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/GuiceContainer.html
>>> ).
>>>
>>> Hope that helps,
>>>
>>> Eelco
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "google-guice" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<google-guice%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/google-guice?hl=en.
>>>
>>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to