Howto make a dynamic web module with sitebricks and annotations?

66 views Asked by At

I'm trying to create a dynamic web module with sitebricks, without web.xml. (Annotations only.) I have been searching the net for good examples, but all of them are simple java applications containing an embedded jetty web server.

I have already created a context listener, just for testing:

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.sitebricks.SitebricksModule;

public class AppConfig extends GuiceServletContextListener {

    @Override
    public Injector getInjector() {
        return Guice.createInjector(new SitebricksModule() {
            @Override
            protected void configureSitebricks() {
                at("/api/version").serve(APIVersion.class);
            }
        });
    }
}

The APIVersion class:

@Service
public class APIVersion {
     public class APIVersionResult {
         public int major = 1;
         public int minor = 0;
     }

     @Get
     Reply<APIVersionResult> get() {
       return Reply.with(new APIVersionResult()).as(Json.class);
     }   
}

Also created a Filter, but maybe I don't need it:

import com.google.inject.servlet.GuiceFilter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter("/*")
public class AppFilter extends GuiceFilter implements Filter {

    public AppFilter() {
        super();
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request, response);
    }

    public void init(FilterConfig fConfig) throws ServletException {
    }

}

The project has Dynamic Web Module facet. I can compile it into a war file. But I always get a 404 error for http://localhost/api/version

What am I missing?

1

There are 1 answers

0
nagylzs On

Here is the answer! I was missing a single thing: the @WebListener annotation was not added to my GuiceServletContextListener!

@WebListener
public class AppConfig extends GuiceServletContextListener {

    @Override
    public Injector getInjector() {
        return Guice.createInjector(new SitebricksModule() {
            @Override
            protected void configureSitebricks() {
                at("/api/version").serve(APIVersion.class);
            }
        });
    }
}