Use new version of Jackson JSON with Jersey

Guice Module:

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.google.inject.Singleton;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
 
public final class JerseyModule extends JerseyServletModule
{
   @Override
   protected void configureServlets()
   {
      bind(JacksonJaxbJsonProvider.class).in(Singleton.class);
      filter("/*").through(GuiceContainer.class);
   }
}

And the Maven pom:

<dependency>
   <groupId>com.fasterxml.jackson.jaxrs</groupId>
   <artifactId>jackson-jaxrs-json-provider</artifactId>
   <version>2.0.5</version>
</dependency>

Jersey + Guice ExceptionMapper

import com.google.inject.Singleton;
 
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
 
@Provider
@Singleton
public class MyExceptionMapper implements ExceptionMapper<MyException>
{
   @Override
   public Response toResponse(final MyException exception)
   {
      return Response.status(Status.NOT_FOUND).entity(exception.getMessage()).build();
   }
}
import com.google.inject.AbstractModule;
 
public class MyModule extends AbstractModule
{
   @Override
   protected void configure()
   {
      bind(MyExceptionMapper.class);
   }
}

Integrating guice and junit4

A JUnit4 Method rule to perform injection on your test fixture before each test case:

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
 
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
 
public class PerTestGuiceRule implements MethodRule
{
   private final Module module;
 
   public PerTestGuiceRule(final Module module)
   {
      this.module = module;
   }
 
   @Override
   public Statement apply(
            final Statement base,
            @SuppressWarnings("unused") final FrameworkMethod method,
            final Object target)
   {
      return new Statement()
      {
         @Override
         public void evaluate() throws Throwable
         {
            final Injector injector = Guice.createInjector(module);
 
            injector.injectMembers(target);
 
            base.evaluate();
         }
      };
   }
}

Use it by putting this code in you test fixture:

@Rule public PerTestGuiceRule guiceRule = 
                   new PerTestGuiceRule(new MyModule());
 
@Inject MyInjectableType myInjectableType;

You might want to check out GuiceBerry. I tried to use it, but for my case it seems very complex, or maybe impossible, to create your module per-test due to the static map of module class -> module instance used internally by guiceberry. There is a test scope included, but I don’t see a really clean way to use that if you want to test your production guice modules.