C-Otto
C-Otto

Reputation: 5843

Injecting EJB within JAX-RS resource in JBoss 5

Although there already are quite some StackOverflow questions, blog entries, etc. on the web, I still cannot figure out a solution to the problem stated below.

Similar to this question (Injecting EJB within JAX-RS resource on JBoss7) I'd like to inject a EJB instance into a JAX-RS class. I tried with JBoss 5, JBoss 7, and WildFly 8. I either get no injection at all (field is null), or the server does not deploy (as soon as I try to combine all sorts of annotations). Adding @Stateless to the JAX-RS makes the application server know both classes as beans. However, no injection takes place.

Is there a way to inject EJBs into a REST application? What kind of information (in addition to that contained in the question linked to above) could I provide to help?

EDIT: I created a Github project showing code that works (with Glassfish 4.0) and does not work (with JBoss 5).

https://github.com/C-Otto/beantest

EDIT2: Running the Code which I tried on JBoss 5 on Glassfish 4.0 gives:

Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:403)

EDIT3: The crucial information might be that I'd like a solution that works on JBoss 5

Upvotes: 0

Views: 1195

Answers (1)

Shay Elkayam
Shay Elkayam

Reputation: 4158

If you don't want to make your JAX-RS resource an EJB too (@Stateless) and then use @EJB or @Resource to inject it, you can always go with JNDI lookup (I tend to write a "ServiceLocator" class that gets a service via its class.

A nice resource to read about the topic:

https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project

A sample code:

try {
     // 1. Retreive the Home Interface using a JNDI Lookup
     // Retrieve the initial context for JNDI.       // No properties needed when local
      Context context = new InitialContext();

      // Retrieve the home interface using a JNDI lookup using
      // the java:comp/env bean environment variable        // specified in web.xml
      helloHome = (HelloLocalHome) context.lookup("java:comp/env/ejb/HelloBean");

     //2. Narrow the returned object to be an HelloHome object.      // Since the client is local, cast it to the correct object type.
     //3. Create the local Hello bean instance, return the reference 
      hello = (HelloLocal)helloHome.create();

    } catch(NamingException e) {

    } catch(CreateException e) {

    }

This is not "injecting" per-se, but you don't use "new" as-well, and you let the application server give you an instance which is managed.

I hope this was useful and I'm not telling you something you already know!

EDIT:

This is an excellent example: https://docs.jboss.org/author/display/AS72/EJB+invocations+from+a+remote+client+using+JNDI

EDIT 2:
As you stated in your comment, you'd like to inject it via annotations. If the JNDI lookup is currently working for you without problems, and If you're using Java EE 6+ (which I'm guessing you are), you can do the following:

@EJB(lookup = "jndi-lookup-string-here")
private RemoteInterface bean;

Upvotes: 2

Related Questions