4 Replies Latest reply on Apr 10, 2012 6:04 AM by skshyamal

    CDI producer bean with jax-rs dependency

    skshyamal

      I have treid a example with weld as CDI container with jersey as jax-rs implementation for the dependency of jax-rs resources on CDI producer bean. Below is my CDI producer class which priduces unique id depends on session id or user provided id through REST path ..

       

      public class UuidProducer {

         

          @Inject

          private Logger log;

         

          @Context

          private HttpServletRequest request;

         

          @PathParam("uuid")

          private String uuid;

       

       

          @Produces

          @Uuid

          public String produceUuid() {

              if (StringUtils.isNotBlank(uuid)) {

                  return uuid;

              }

              if (request.getSession() != null) {

                  return request.getSession().getId();

              }

              log.info("Generating UUID...");

              return UUID.randomUUID().toString();

          }

       

       

      }

       

      When I tried this with Jboss AS7 with RestEasy implementation no jax-rs resource is injected into this CDI class. My question is that if how can I inject jax-rs resource in normal CDI bean?

        • 1. Re: CDI producer bean with jax-rs dependency
          jharting

          JAX-RS injection is not available in plain CDI beans.

          • 2. Re: CDI producer bean with jax-rs dependency
            skshyamal

            But it works in Glassfish server which uses WELD for CDI and jersey for JAX-RS. Intersting class I found in jersey is com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer which bridges the CDI and JAX-RS beans.

            • 3. Re: CDI producer bean with jax-rs dependency
              jharting

              CDI an JAX-RS are bridged in JBoss AS 7 as the specification requires. You can use any CDI component as a JAX-RS resource or provider. This involves dependency injection and other CDI services. The injection you describe works the other way around and is not portable. In your code, you should still be able to get a reference to HttpServletContext and get the information you need from there. Alternatively, you'll have to inject the information you need into a JAX-RS resource/provider and put a CDI producer there.

              • 4. Re: CDI producer bean with jax-rs dependency
                skshyamal

                If I put a CDI producer in JAX-RS resource then there is a chance of cyclic injection problem. For ex. I have CDI bean with constructor parameter as String which is nothing but the Path variable from REST resource bean. So I create a producer method to expose this path variable in JAX-RS resource bean, then comes the cyclic dependency problem.