1

I am not able to inject the cdi bean in Resteasy. While debugging it always seems to show null pointer exception. ie 'jaxRsImpl' in the below code is always null. I am trying to run on jboss eap 6.2

@Path("/jaxrs-service")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class JAXRSService {

    @Inject
    private JAXRSImpl jaxRsImpl;

    @POST
    @Path("/authenticate")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response authenticate(Credentials user) {
           return jaxRsImpl.authenticate(user);
    }

}

And the class which i intend to inject is

@RequestScoped
public class JAXRSImpl {
    public Response authenticate(Credentials user) {
    // Some logic

    }
}

As my application is web so i have added beans.xml inside WEB-INF folder

My Initialiser looks like

@ApplicationPath("/rest")
public class JAXRSInitializer extends Application {
   private Set<Object> singletons = new HashSet<Object>();
   private Set<Class<?>> classes = new HashSet<Class<?>>();

   public JAXRSInitializer() {
      singletons.add(new JAXRSService());
      classes.add(JAXRSImpl.class);
   }

   @Override
   public Set<Class<?>> getClasses() {
     return classes;
   }

   @Override
   public Set<Object> getSingletons() {
     return singletons;
   }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Licht
  • 13
  • 6

1 Answers1

5

You need to ensure that your application is CDI aware. Here are some of the key requirements:

  • In JAX-RS, don't list out your classes/singletons. Allow the container to discover them. Basically create an empty Application implementation.
  • Make sure you have a valid beans.xml
  • Make sure your rest endpoints have a valid scope - e.g. @RequestScoped

The first bullet is key, since you're manually instantiating your service, rather than allowing the container to find them.

John Ament
  • 11,595
  • 1
  • 36
  • 45