使用CDI在TJWS上进行RESTEasy

时间:2012-05-12 14:56:19

标签: cdi resteasy tjws

我尝试使用TJWS Embeddable Servlet Container使用此用户指南http://docs.jboss.org/resteasy/docs/2.3.3.Final/userguide/html/RESTEasy_Embedded_Container.html#d0e2640

启动RestEasy应用程序

JBOSS7-AS中的应用程序工作正确。我想使用TJWS进行调试和单元测试,但是依赖注入有问题。

我创建了资源类UserResource,它使用CDI注入实用程序类UserManager:

@Path("users")
@SessionScoped
class UserResource {

  @Inject
  UserManager userManager; // simple interface and imlementation

  public UserResource() {} // constructor with no parameters for bean

    @Path("list")
    @GET
    public List<User> list() {
       List<User> userList = userManager.getList(); // NullPointerException
       return userList;
    }
}

在main中启动TJWS:

public static void main(String[] args) throws IOException {
    TJWSEmbeddedJaxrsServer tjws = new TJWSEmbeddedJaxrsServer();
    tjws.setPort(9997);
    tjws.start();

    tjws.getDeployment().getRegistry().addPerRequestResource(User.class);
}

当我尝试通过浏览器获取http://localhost/users/list时,我在UserResource.list()方法中得到NullPointerException,因为userManager未注入且为null。

有没有办法注入userManager?

1 个答案:

答案 0 :(得分:0)

TJWS是一个独立的servlet容器和Web服务器,不支持@Inject等EE注释。要使代码工作,您必须使用诸如JBoss AS之类的EE容器。