HK2 is not injecting the HttpServletRequest with jersey

You should @Override protected DeploymentContext configureDeployment() in the JerseyTest to return a ServletDeploymentContext. For example import javax.inject.Inject; import javax.inject.Provider; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Response; import org.glassfish.hk2.api.Factory; import org.glassfish.hk2.utilities.binding.AbstractBinder; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.servlet.ServletContainer; import org.glassfish.jersey.test.DeploymentContext; import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.ServletDeploymentContext; import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; import org.glassfish.jersey.test.spi.TestContainerException; import org.glassfish.jersey.test.spi.TestContainerFactory; import org.junit.Test; public class ServletTest extends … Read more

How do I properly configure an EntityManager in a jersey / hk2 application?

One option is to instead of creating a new EntityManagerFactory in the EMFactory (which is in a request scope), you could create a singleton factory for the EntityManagerFactory, then just inject the EntityManagerFactory into the EMFactory. public class EMFFactory implements Factory<EntityManagerFactory> { private final EntityManagerFactory emf; public EMFFactory (){ emf = Persistence.createEntityManagerFactory(persistenceUnit); } public EntityManagerFactory … Read more

Jersey 2.x Custom Injection Annotation With Attributes

Yeah Jersey made the creation of custom injections a bit more complicated in 2.x. There are a few main components to custom injection you need to know about with Jersey 2.x org.glassfish.hk2.api.Factory – Creates injectable objects/services org.glassfish.hk2.api.InjectionResolver – Used to create injection points for your own annotations. org.glassfish.jersey.server.spi.internal.ValueFactoryProvider – To provide parameter value injections. You … Read more

Dependency injection with Jersey 2.0

You need to define an AbstractBinder and register it in your JAX-RS application. The binder specifies how the dependency injection should create your classes. public class MyApplicationBinder extends AbstractBinder { @Override protected void configure() { bind(MyService.class).to(MyService.class); } } When @Inject is detected on a parameter or field of type MyService.class it is instantiated using the … Read more