我想知道为什么ejb注入JAX-RS资源(JBoss7上的RestEasy)无效。 EJB不是战争的一部分,而是它自己的EJB jar,但我认为这不应该是问题。我被迫做了ctx.lookups“解决方法”,这并不漂亮。我是否遗漏了某些东西,或者真的不支持这样注入EJB?以下示例不适用于JBoss,但适用于Glassfish(遗憾的是我必须在JBoss上运行我的应用程序)
Path("x")
@RequestScoped
public class UserResource {
@Inject // CDI not working too
private Service service1;
@EJB
private Service service2;
private Service service3;
@GET
@Path("y")
public Response authenticate(@Context HttpHeaders headers) {
System.out.println("null == " + service1);
System.out.println("null == " + service2);
service3 = annoyingLookup(Service.class);
System.out.println("null != " + service3);
}
private <T> T annoyingLookup(Class<T> clazz) {
...
ctx.lookup("java:app/module/" + classzz.getSimpleName());
}
答案 0 :(得分:2)
以下内容对我有用。
RESTEasy根上下文:
package com.foo.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest")
public class RestServiceLocator extends Application {
}
STLS bean:
package com.foo.rest;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
@Path("/foo")
@Stateless
@LocalBean //Note: @Local(FooResource.class) does not let RESTEasy to load EJB!
public class FooResourceBean implements FooResource {
@EJB
private FooResourceBean h; // Works!
/**
* http://localhost:8080/webapp/rest/foo/baa
*
*/
@GET
@Path("/baa")
@Produces("application/json")
@Override
public Response baa() {
String json = "{ \"baa\" : [ " +
" { \"icon\":\"source1.png\" , \"description\":\"Source 1\" }, " +
" { \"icon\":\"source2.png\" , \"description\":\"Source 2\" }, " +
" { \"icon\":\"source3.png\" , \"description\":\"Source 3\" } " +
" ]}\";";
return Response.ok(json).build();
}
}
答案 1 :(得分:0)
添加
@Stateless
到JAX-RS资源
@Path("/")
@Stateless
public class MainMain
{
@EJB(mappedName = "java:global/optima-wsi/OptimaConfigurator!com.sistemaits.optima.configurator.IOptimaConfigurator")
IOptimaConfigurator configurator;
---
}