Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2014 Zach Melamed
   * Latest version available online at
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package com.tectonica.jee5;
 import java.util.Map;
 import  com.sun.jersey.api.core.PackagesResourceConfig;
 import  com.sun.jersey.api.core.ResourceConfig;
 public class GuiceRestModule extends ServletModule
 	protected void configureServlets()
 		// add support for the @PostConstruct annotation for Guice-injected objects
 		// if you choose to remove it, also modify GuiceJsfInjector.invokePostConstruct()
 		bindListener(Matchers.any(), new PostConstructTypeListener(null));
 		// configure Jersey: use Jackson + CORS-filter
 		Map<StringStringinitParams = new HashMap<>();
 		if (isUseJackson())
 		String reponseFilters = getResponseFilters();
 		if (reponseFilters != null)
 		// route all requests through Guice

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())

override this to return a (comma-delimited) list of Jersey's ContainerResponseFilters. By default returns the CorsFilter.
 	protected String getResponseFilters()
 		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. For example:
	protected void doCustomBinds()

override to add additional Guice configuration. For example, to have non-REST servlets served through Guice, use:
	protected void doCustomServing()

override to change the context-parameters passed to Jersey's servlet. For example:
 initParams.put("com.sun.jersey.config.feature.Trace", "true");
	protected void doCustomJerseyParameters(Map<StringStringinitParams)
New to GrepCode? Check out our FAQ X