【发布时间】:2015-09-22 08:04:12
【问题描述】:
我阅读了很多关于使用 jax rs 2.0 尤其是球衣进行注入的可能性。
我什至在 jax rs 2.0 规范中读到了 ejb 注入。但是我在网上阅读的各种帖子中仍然没有找到一个独特的解决方案。
在我正在使用的用例中:
WildFly 9.0 和 Jersey 2.x
我有一个 web 应用程序公开我的 REST 服务并导入一个实现我的模型数据的 jar。
这是 CDI 方法:
@RequestScoped
@Path("/myPath")
public class ModelRetriever {
@Context
SecurityContext securityContext;
@Inject
private IMyModel MyModel;
@Path("{i}")
@GET
@Produces("application/json")
public Response countries(@PathParam("i") String countryId)
throws JSONException, Failure, IOException {
MyModel.doSomething();
}
这是我的 IMyModel 界面
public interface IKasPrincipal extends Principal {
public void doSomething();
}
这是 MyModel 的实现:
@RequestScope
public class MyModelImpl implements IMyModel {
public void doSomehting() {
doSomething();
}
}
我尝试的另一种方法是使用 EJB 注入来更改我以前的注释,如下所示:
@Stateless
@Path("/myPath")
public class ModelRetriever {
@EJB
private IMyModel MyModel;
@Path("{i}")
@GET
@Produces("application/json")
public Response countries(@PathParam("i") String countryId)
throws JSONException, Failure, IOException {
MyModel.doSomething();
}
这是我的 IMyModel 界面
@Local
public interface IKasPrincipal extends Principal {
public void doSomething();
}
这是 MyModel 的实现:
@Stateless
public class MyModelImpl implements IMyModel {
public void doSomehting() {
doSomething();
}
}
我使用 EJB 方法得到一个空对象,我使用 CDI 得到这个异常
Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=IMyModel,parent=ModelRetriever,qualifiers={},position=-1,optional=false,self=false,unqualified=null,616459318)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:75)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
那我有什么遗漏的吗?
【问题讨论】:
标签: rest dependency-injection jax-rs jersey-2.0 ejb-3.1