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
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
