Skip to content

Instantly share code, notes, and snippets.

@davetrux
Created March 4, 2014 22:28
Show Gist options
  • Save davetrux/9357113 to your computer and use it in GitHub Desktop.
Save davetrux/9357113 to your computer and use it in GitHub Desktop.
Configuration class for instantiation of Jersey Services with Guice
package com.demo.test.rest;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.demo.DemoRestService;
import com.demo.BasicAuthFilter;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import java.util.HashMap;
import java.util.Map;
public class GuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {
@Override
protected void configureServlets() {
bind(DataSource.class).to(HsqlDataSource.class);
Map<String, String> filterParams = new HashMap<>();
filterParams.put("OpenUrls", "/noauth");
filter("/api/*").through(BasicAuthFilter.class, filterParams);
Map<String, String> initParams = new HashMap<>();
initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
bind(DemoRestService.class);
serve("/api/*").with(GuiceContainer.class, initParams);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment