1 Reply Latest reply on Jul 9, 2013 2:05 PM by Bradford Hovinen

    Injected resource is null in JAX-RS bean

    Bradford Hovinen Newbie

      Hello all,

       

      I'm trying to inject a resource into a JAX-RS bean and the injected resource is always null. During deploy no error is reported.

       

      The source for the JAX-RS bean is:

       

      @ManagedBean

      @Path("/distance")

      public class DistanceService {

        @Inject

                private DistanceRepository repository;

       

                /* (non-Javadoc)

                 * @see com.dpdhl.cac.erc.services.IDistance#getDrivenDistance(java.lang.String, java.lang.String, java.lang.String, java.lang.String)

                 */

                @GET

        public DistanceResult getDrivenDistanceXML(

                                              @QueryParam(value="origCountry") String origCountry,

                   @QueryParam(value="origPostalCode") String origPostalCode,

                                               @QueryParam(value="destCountry") String destCountry,

                                               @QueryParam(value="destPostalCode") String destPostalCode) {

       

            // NullPointerException here because repository is always null

            return repository.getDrivenDistance(origCountry, origPostalCode, destCountry, destPostalCode);

                }

      }

       

      The service to be injected is:

       

      @ApplicationScoped

      public class DistanceRepository {

                @Inject

                private EntityManager em;

       

                public DistanceResult getDrivenDistance(String origCountry, String origPostalCode, String destCountry, String destPostalCode) {...}

      }

       

      The application-class is defined as follows:

      @ApplicationPath("/services")

      public class ERCApplication extends Application {

                private Set<Class<?>> classes = new HashSet<>();

                private Set<Object> singletons = new HashSet<>();

       

                public ERCApplication() {

          singletons.add(new DistanceService());

                }

       

                @Override

                public Set<Object> getSingletons() {

          return singletons;

                }

       

                @Override

                public Set<Class<?>> getClasses() {

          return classes;

                }

      }

       

      I've tried various combinations of annotations to no avail. I can confirm, however, that the @Inject annotation is not being ignored, since some of my attempts caused Weld to complain during deploy.

       

      Can anyone help? Thank you in advance.