5

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

阅读教程后,我写了以下内容:

@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;
        }
}

但是我得到以下异常:

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

怎么了?

4

3 回答 3

7

我在玩 JAXRS 和 @Inject-ing EJB 并得到同样的错误。使用@EJB 效果很好。

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

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

这也可能对您有所帮助。

于 2014-09-02T10:55:25.750 回答
1

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

于 2014-01-26T16:33:42.360 回答
0

检查界面@Contract中的包。ProductManagerJersey ( @Contract ) 和 HK2 ( @Contract ) 都有使用此名称的注释。

请务必查看 Jersey 用户指南:

于 2014-01-21T20:22:34.280 回答