泽西岛(REST)子资源CDI [英] Jersey (REST) Sub-resource CDI

查看:106
本文介绍了泽西岛(REST)子资源CDI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个企业项目,该项目具有在GlassFish v3.1,Weld v1.1和Jersey上运行的EJB模块和Web项目。在EJB中,我定义了一个实体类 Manufacturer 并生成了一个会话外观 ManufacturerFacade

I am working on an enterprise project that has an EJB module and a web project running on GlassFish v3.1, Weld v1.1 and Jersey. In the EJB I have defined an entity class Manufacturer and generated a session facade ManufacturerFacade.

在Web项目中,我希望通过REST公开制造商实例。为此,我创建了以下资源:

In the web project I wish to expose Manufacturer instances through REST. To do so, I created the following resources:

ManufacturersResource 是一个容器资源,返回所有列表制造商存储在数据库中。它通过注入 ManufacturerFacade 并调用 findAll()方法来实现。缩写代码:

The ManufacturersResource is a container resource that returns a list of all manufacturers stored in the database. It does so by injecting the ManufacturerFacade and calling the findAll() method. Abbreviated code:

@RequestScoped
@Path("/manufacturer")
public class ManufacturersResource {

    @Inject
    private ManufacturerFacade manufacturerFacade;

    @GET
    @Produces("application/xml")
    public List<Manufacturer> getManufacturers() {
        return manufacturerFacade.findAll();
    }
}

此资源还有一个子资源:

This resource also has a sub-resource:

@Path("{id}")
public ManufacturerResource getManufacturer(@PathParam("id") String id) {
    return ManufacturerResource.getInstance(id, manufacturerFacade);
}

ManufacturerFacade 看起来如下:

public class ManufacturerResource {

    @Inject
    private ManufacturerFacade manufacturerFacade;

    private long id;

    private ManufacturerResource(String id) {
        this.id = Long.parseLong(id);
    }

    public static ManufacturerResource getInstance(String id,) {
        return new ManufacturerResource(id);
    }

    @GET
    @Produces("application/xml")
    public Manufacturer getManufacturer() {
        return manufacturerFacade.find(id);
    }

}

然而,我们在不同的类中,并且 ManufacturerResource 没有被框架实例化,因此没有 ManufacturerFacade 注入。

We are in a different class however, and the ManufacturerResource is not being instantiated by the framework and thus does not have the ManufacturerFacade injected.

我知道我可以简单地将容器资源( ManufacturersResource )的外观传递给项目资源( ManufacturerResource )通过构造函数,但是有可能以某种方式让DI在它们上面工作,或者在构造函数中传递一个非常好的解决方案吗?

I know I can simply pass the facade from the container resource (ManufacturersResource) to the item resource (ManufacturerResource) through the constructor but is it possible to somehow get DI working on them as well or is passing it through the constructor a perfectly fine solution here?

谢谢!

推荐答案

你应该可以使用ResourceContext来传递id使用二传手。如果它不起作用,请提交一个错误(http://java.net/jira/browse/JERSEY)。

You should be able to use ResourceContext for this and pass the id using a setter. Please file a bug if it does not work (http://java.net/jira/browse/JERSEY).

@Context
private ResourceContext resourceContext;

@Path("{id}")
public ManufacturerResource getManufacturer(@PathParam("id") String id) {
    ManufacturerResource r = resourceContext.getResource(ManufacturerResource.class);
    r.setId(id);
    return r;
}  

这篇关于泽西岛(REST)子资源CDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆