0

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

  • Commit 4bf2f3d23f49d106a435f068ed9b30701bbedc9d works using Glassfish 4.0.
  • Commit 50d137674e55e1ceb512fe0029b9555ff7c2ec21 uses Jersey 1.8, which does not work.
  • Commit 86004b7fb6263d66bda7dd302f2d2a714ff3b939 uses Jersey 2.6, which also does not work.

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

Community
  • 1
  • 1
C-Otto
  • 5,615
  • 3
  • 29
  • 62
  • please post the relevant code. – Camilo Feb 27 '14 at 17:58
  • I thought this guy explained it pretty well http://stackoverflow.com/questions/3027834/inject-a-ejb-into-jax-rs-restfull-service – ZeusSelerim Feb 27 '14 at 20:03
  • actually there is a way and basically you don't have to do anything special to achieve it. Your JAX-RS resource should be an EJB itself and that is enough. Problem must be somewhere in your code. So I would agree with Camilo here. Seeing your code might help to understand the problem – jjd Feb 28 '14 at 13:40
  • I added the code, please have another look. Thanks! – C-Otto Feb 28 '14 at 16:40
  • Jesus Mireles, I tried that before, sadly it does not help. – C-Otto Feb 28 '14 at 16:40

1 Answers1

2

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;
Shay Elkayam
  • 4,128
  • 1
  • 22
  • 19
  • Sadly, this is our current work around approach. I _do_ want to use annotations and adding @Stateless or anything similar did not help. – C-Otto Mar 09 '14 at 10:13