Search code examples
javaservletswicketguice-servlet

Integrating Guice, Servlets And Wicket


I've spent a few days now looking through all the examples I can find and I have not found one that has worked for me. Most seem to be 4-6 years old now. I feel like I'm just missing something small to get my servlets and wicket application to work together with Guice. So far this configuration works with my wicket pages but my servlets are unable to Inject anything. Any help would be appreciated.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

        <display-name>project</display-name>

        <listener>
            <listener-class>com.project.servletlistener.ServletContextListener</listener-class>
        </listener>

        <filter>
            <filter-name>guiceFilter</filter-name>
            <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
        </filter>

        <filter-mapping>
            <filter-name>guiceFilter</filter-name>
            //Excuse the '' around the / and *. 
            //Without them it is creating a comment in stackoverflow and 
            //they are not present in my actual web.xml
            <url-pattern>'/*'</url-pattern>
        </filter-mapping>

    </web-app>


    public class ServletContextListener extends GuiceServletContextListener 
    {

        @Override
        protected Injector getInjector() {
            return Guice.createInjector(createServletModule());
        }

        private ServletModule createServletModule() {
            return new ServletModule() {

                @Override
                protected void configureServlets() {

                    //One example said to include
                    //filter("/*").through(PersistFilter.class); but this resulted  
                    //in Guice configuration errors. No implementation for 
                    //com.google.inject.persist.PersistService was bound. 

                    Map<String, String> params = new HashMap<String, String>();  
                    params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
                    params.put("applicationClassName", "com.project.application.WicketApplication");

                    filter("/*").through(WicketFilter.class, params);
                    bind(WicketFilter.class).in(Singleton.class);
                    bind(WebApplication.class).to(WicketApplication.class);
                }
            };
        }
    }

Solution

  • This is what ended up allowing me to use Guice in my servlets and still have my Wicket Code work. This was a simple error that turned out to be my lack of understanding of Servlets. All that needed to be done was to register the servlets with this context.

    private ServletModule createServletModule() {
        return new ServletModule() {
            @Override
            protected void configureServlets() {
    
                Map<String, String> params = new HashMap<String, String>();  
                params.put(WicketFilter.FILTER_MAPPING_PARAM, "/*");
                params.put("applicationClassName", "com.project.application.WicketApplication");
    
                bind(AuthorizeServlet.class);
                bind(AuthorizeCallbackServlet.class);
                bind(WicketFilter.class).in(Singleton.class);
                bind(WebApplication.class).to(WicketApplication.class);
    
                serve("/authorize").with(AuthorizeServlet.class);
                serve("/authorizecallback").with(AuthorizeCallbackServlet.class);
                filter("/*").through(WicketFilter.class, params);
            }
    
        };