Java org.glassfish.hk2.api.UnsatisfiedDependencyException: Injectee 没有可供注入的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21265285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:46:40  来源:igfitidea点击:

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

javadependency-injectionjerseyjersey-2.0hk2

提问by Dejell

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

我是 Jersey 2 的新手。到目前为止,我使用 Jersey 1.x 和 Spring,并希望使用 HK2 实现。

After reading the tutorialI 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?

怎么了?

回答by Michal Gajdos

Check the package of the @Contractin your ProductManagerinterface. Both Jersey (@Contract) and HK2 (@Contract) have annotation with this name.

检查的包@Contract在你的ProductManager界面。Jersey ( @Contract) 和 HK2 ( @Contract) 都有这个名字的注释。

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

请务必查看 Jersey 用户指南:

回答by jwells131313

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

我假设上面的@Service 注释是 hk2 @Service,在这种情况下,您应该知道 @Service 在 Jersey 中不会自动工作。相反,您需要在某些 Jersey 绑定器中添加一个类似于 bind(MyProductManager).to(ProductManager) 的绑定

回答by Boris

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

我在玩 JAXRS 和 @Inject-ing EJB 并遇到同样的错误。使用@EJB,它运行良好。

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

解决方案是添加 CDI 配置文件并将 bean-discovery-mode="annotated" 更改为 bean-discovery-mode="all"

After that I could use @Inject with my EJB.

之后,我可以将@Inject 与我的 EJB 一起使用。

This might help you also.

这也可能对您有所帮助。