Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.tectonica.jersey1;
  
  import java.util.HashMap;
  import java.util.Map;
  
 import  com.sun.jersey.api.core.PackagesResourceConfig;
 import  com.sun.jersey.api.core.ResourceConfig;
Servlet-listener for intercepting REST requests and pass them through Guice before Jersey

Author(s):
Zach Melamed
 
 {
 	protected final GuiceRestModule module;
 
 	public GuiceRestListener()
 	{
 		 = new GuiceRestModule();
 	}
 
 	public GuiceRestListener(GuiceRestModule customModule)
 	{
 		 = customModule;
 	}
 
 	protected Injector getInjector()
 	{
 		return Guice.createInjector();
 	}
 
 	public static class GuiceRestModule extends ServletModule
 	{
 		protected void configureServlets()
 		{
 
 
 			// add support for the @PostConstruct annotation for Guice-injected objects
 			bindListener(Matchers.any(), new PostConstructTypeListener(null));
 
 			// configure Jersey: use Jackson + CORS-filter
 			Map<StringStringinitParams = new HashMap<>();
 			if (isUseJackson())
 				initParams.put("com.sun.jersey.api.json.POJOMappingFeature""true");
 			String reponseFilters = getResponseFilters();
 			if (reponseFilters != null)
 				initParams.put("com.sun.jersey.spi.container.ContainerResponseFilters"reponseFilters);
 //			initParams.put("com.sun.jersey.config.feature.Trace", "true");
 
 			// route all requests through Guice
 			serve(getServingUrl()).with(GuiceContainer.classinitParams);
 
 		}

override this to specify the URL-pattern of the REST service
 
 		protected String getServingUrl()
 		{
 			return "/*";
 		}

override this to specify a root-package under which all your JAX-RS annotated class are located
 
 		protected String getRootPackage()
 		{
 			return null;
 		}

bind JAX-RS annotated classes to be served through Guice. based on a recursive class scanning that starts from the package returned by getRootPackage(). override this if you wish to avoid the scanning and bind your classes explicitly
 
 		protected void bindJaxrsResources()
 		{
 			String rootPackage = getRootPackage();
 			if (rootPackage == null)
 				throw new NullPointerException(
 						"to scan for JAX-RS annotated classes, either override getRootPackage() or bindJaxrsResources()");
 
 			ResourceConfig rc = new PackagesResourceConfig(rootPackage);
 			for (Class<?> resource : rc.getClasses())
 				bind(resource);
 		}

override this to return a (comma-delimited) list of Jersey's ContainerResponseFilters
		{
			return CorsFilter.class.getName();
		}

override this to avoid usage of Jackson
		protected boolean isUseJackson()
		{
			return true;
		}

override to perform application-logic bindings, typically between interfaces and concrete implementations
		protected void doCustomBinds()
		{
//			bind(MyIntf.class).to(MyImpl.class);
		}

override to perform custom (i.e. non-REST) serving through Guice
		protected void doCustomServing()
		{
//			serve("/my/*").with(MyServlet.class);
		}
	}
New to GrepCode? Check out our FAQ X