How to make a dynamic web module with sitebricks and annotations?

12 views
Skip to first unread message

László Nagy

unread,
Dec 20, 2017, 1:49:14 AM12/20/17
to Google Sitebricks
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.

But I would like to run this on tomcat!

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

Someone told me that tomcat does not have a JAX-RS implementation, so maybe this could be a problem?

What am I missing?

Reply all
Reply to author
Forward
0 new messages