5

I am new to Jersey 2. So far I worked with Jersey 1.x and Spring and would like to use HK2 implementation.

After reading the tutorial I wrote the following:

@ManagedBean
@Path("products")
@Produces({ MediaType.APPLICATION_JSON })
public class ProductResource {

    @Inject
    ProductManager productManager;

    @GET
    public GenericResponseData<List<Product>> getProducts(@QueryParam("condition") Condition condition, @QueryParam("keywords") String keywords) {
        GenericResponseData<List<Product>> res = new GenericResponseData<List<Product>>();
        res.setObject(productManager.getProducts(condition, keywords));
        return res;
    }

}
@Contract
public interface ProductManager {
    public List<Product> getProducts(Condition condition, String keywords);
}

@Service
public class MyProductManager implements ProductManager {
    @Override
    public List<Product> getProducts(Condition condition, String keywords) {
            return null;
        }
}

However I get the following exception:

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee

What is wrong?

Dejell
  • 13,947
  • 40
  • 146
  • 229

3 Answers3

7

I was playing around with JAXRS and @Inject-ing EJB and got same error. With @EJB it worked fine.

The solution was to add CDI configuration file and change bean-discovery-mode="annotated" to bean-discovery-mode="all"

After that I could use @Inject with my EJB.

This might help you also.

Boris
  • 81
  • 1
  • 2
  • This solution stops raising the exception, but the injected object will be null!!! (At least for me) – ehsun7b Sep 21 '14 at 11:45
  • Works for me with same erreor as poster and is what the spec says. Just forgot it in my beans.xml. – ymajoros Aug 08 '15 at 09:26
  • Is it possible to @Inject without a beans.xml file? I would like to write the application using strictly Java configuration as I do with my Spring applications. – Gilbert Lopez Jan 24 '17 at 03:41
  • Just found you can use @RequestScoped annotation, e.g. – Gilbert Lopez Jan 24 '17 at 06:20
  • Thanks, it worked for me., for those who want to know how to add it: https://javaee.github.io/tutorial/cdi-adv001.html – Given Feb 15 '22 at 12:12
1

I assume that the @Service annotation above is the hk2 @Service in which case you should know that @Service does not work automatically in Jersey. Instead you would need to add a binding that would do like a bind(MyProductManager).to(ProductManager) in some Jersey binder

jwells131313
  • 2,364
  • 1
  • 16
  • 26
  • ye, I added the binding but wondering why the @Service doesn't work – Dejell Jan 26 '14 at 20:27
  • 1
    Jersey has made the choice to use explicit bindings over automatic bindings. This is a perfectly valid choice, as it provides more control to the user, but means that some of the automatic features of hk2 (i.e. @Service) will not work – jwells131313 Jan 27 '14 at 14:08
  • is there a way to keep it? as it gives many abilites to play aronund with the code? – Dejell Jan 27 '14 at 14:34
  • If you have used the hk2-inhabitant-generator to create the inhabitant files at build time (based on the @Service annotation) then you can use code like this to have it automatically read in: `DynamicConfigurationService dcs =locator.getService(DynamicConfigurationService.class); Populator populator = dcs.getPopulator(); populator.populate(new ClasspathDescriptorFileFinder(getClass().getClassLoader());` – jwells131313 Jan 27 '14 at 19:02
  • If you have used the hk2-inhabitant-generator to create the inhabitant files at build time = what does it mean? – Dejell Jan 27 '14 at 19:55
  • if I would use @Service of Jersey - would it work? cuz I tried and it doesn't work – Dejell Jan 27 '14 at 19:56
  • To use the automatic facility of hk2 you need to run a step at building that will scan all of your class files for those marked with @Service and add a resource to the jar with (basically) the list of classes. More information can be found here: [link](https://hk2.java.net/2.2.0/inhabitant-generator.html) – jwells131313 Jan 28 '14 at 15:14
0

Check the package of the @Contract in your ProductManager interface. Both Jersey (@Contract) and HK2 (@Contract) have annotation with this name.

Make sure to take a look also at Jersey User Guide:

Michal Gajdos
  • 10,339
  • 2
  • 43
  • 42