Jersey (REST) Sub-resource CDI(Jersey (REST) 子资源 CDI)
问题描述
我正在开发一个企业项目,该项目有一个 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;
}
这篇关于Jersey (REST) 子资源 CDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jersey (REST) 子资源 CDI


基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01