Jersey (REST) 子资源 CDI

2022-01-21 00:00:00 rest jakarta-ee java jersey

我正在开发一个企业项目,该项目有一个 EJB 模块和一个在 GlassFish v3.1、Weld v1.1 和 Jersey 上运行的 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 公开 Manufacturer 实例.为此,我创建了以下资源:

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 并使用 setter 传递 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;
}  

相关文章