RESTEasy on TJWS with CDI

1k Views Asked by At

I try to use TJWS Embeddable Servlet Container to start RestEasy application using this userguide http://docs.jboss.org/resteasy/docs/2.3.3.Final/userguide/html/RESTEasy_Embedded_Container.html#d0e2640

Application work correct in JBOSS7-AS. I want to use TJWS for debuging and unit testing, but have problem with dependency injection.

I create resource class UserResource, which using CDI to inject utility class 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;
    }
}

Start TJWS in main:

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

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

When i try to get http://localhost/users/list via browser, i get NullPointerException in UserResource.list() method, because userManager not injected and is null.

Is there any way to inject userManager?

1

There are 1 best solutions below

0
On BEST ANSWER

TJWS is a standalone servlet container and web server that does not support EE annotations like @Inject. For the code to work you have to use EE container such as JBoss AS.