users@grizzly.java.net

Re: Grizzly 2 + Guice + Jersey

From: Christopher Piggott <cpiggott_at_gmail.com>
Date: Thu, 7 Jun 2012 16:05:03 -0400

I still don't have this working. I would like to ask some questions
that maybe will lead me to the right solution.

With Jersey + Guice, what I normally do is set up a filter
(GuiceFilter) and map it to "/*" ... and add a
GuiceServletContextListener., The context listener creates the main
injector using whatever guice modules you tell it; the modules usually
end with something like:

                serve("*").with(GuiceContainer.class);

To create an entire server, the trick in 1.9.4 is to do this:

                ServletAdapter adapter = new ServletAdapter();
                adapter.addServletListener( MyContextListener.class.getName() );

                GuiceFilterfilter = new GuiceFilter();
                adapter.addFilter( filter, "guiceFilter", null );

                GrizzlyWebServerws = new GrizzlyWebServer( 8888, ".", use_ssl );
                GrizzlyWebServerProvider.reset( ws );

                ws.addGrizzlyAdapter( adapter, new String[] { "/" } );


So the more refined (perhaps) question is how to approximate this with
grizzly 2. What I tried so far, that doesn't work, is this:

                HttpServer ws = HttpServer.createSimpleServer(".", 8890);
                
                WebappContext ctx = new WebappContext("hub", "/");
                ctx.addListener(CustHubContextListener.class);

                FilterRegistration reg = ctx.addFilter("gf", GuiceFilter.class);
                /* Add filter mapping - how? */

                ctx.deploy(ws);

When I do this, I indeed get a server up and running, but it doesn't
actually answer amy queries. It says:


15:50:22.013 INFO o.g.grizzly.servlet.WebappContext - [hub] Filter
[com.google.inject.servlet.GuiceFilter] registered for url pattern(s)
[[]] and servlet name(s) [[]]
15:50:22.013 INFO o.g.grizzly.servlet.WebappContext - Application
[hub] is ready to service requests. Root: [/].
15:50:22.064 INFO o.g.g.http.server.NetworkListener - Started
listener bound to [0.0.0.0:8890]
15:50:22.067 INFO o.g.grizzly.http.server.HttpServer - [HttpServer] Started.


but two things go wrong. First, I dno't know how to map the filter to
"/*" ... a FilterRegistration doesn't have an .addMapping(). it has:

                addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes,
String[] servletNames)

which I don't understand (it takes an EnumSet?) ... Second, even
without regard to this, it never actually invoked my context listener,
and I'm not sure why. (maybe a result of the filter never getting
mapped?)