3

I'm wondering why ejb injection into JAX-RS resource (RestEasy on JBoss7) is not working. EJBs are not part of war but its own EJB jar but I supposed this should not be the problem. I'm forced to do the ctx.lookups "workaround", which are not pretty. Am I missing something or it is really not supported to inject EJB like that? Example below does not work with JBoss, but works with Glassfish (sadly I gotta run my application on JBoss)

Path("x")
@RequestScoped
public class UserResource {

    @Inject // CDI not working too
    private Service service1;
    @EJB
    private Service service2;

    private Service service3;


    @GET
    @Path("y")
    public Response authenticate(@Context HttpHeaders headers) {
         System.out.println("null == " + service1);
         System.out.println("null == " + service2);

         service3 = annoyingLookup(Service.class);
         System.out.println("null != " + service3);
    }

    private <T> T annoyingLookup(Class<T> clazz) {
       ...
       ctx.lookup("java:app/module/" + classzz.getSimpleName());
    }
zdenda.online
  • 2,451
  • 3
  • 23
  • 45

2 Answers2

2

The following is working for me.

RESTEasy root context:

package com.foo.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class RestServiceLocator extends Application {

}

STLS bean:

package com.foo.rest;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

@Path("/foo")
@Stateless
@LocalBean //Note: @Local(FooResource.class) does not let RESTEasy to load EJB!
public class FooResourceBean implements FooResource {

    @EJB 
    private FooResourceBean h; // Works!

    /**
     * http://localhost:8080/webapp/rest/foo/baa
     *  
     */
    @GET
    @Path("/baa")
    @Produces("application/json")
    @Override
    public Response baa() {

        String json = "{ \"baa\" : [ " +
                      "             { \"icon\":\"source1.png\" , \"description\":\"Source 1\" }, " +
                      "             { \"icon\":\"source2.png\" , \"description\":\"Source 2\" }, " +
                      "             { \"icon\":\"source3.png\" , \"description\":\"Source 3\" } " +
                      "          ]}\";";

        return Response.ok(json).build();
    }

}
Bruno Santos
  • 1,530
  • 1
  • 15
  • 22
  • Thanks for answer penguin. Well your answer looks same as my example. Except that you are using Stateless and LocalBean annotations. Im not sure if this is right way to solve this. I don't want my resource to be EJB – zdenda.online Jan 07 '12 at 19:34
  • It's not, I absolutely agree with you. There are issues in RESTEasy on JBoss 7 (all was working just fine on JBoss 6) and I've just tried to provide a possible solution. I would rather use CDI myself but it is not working. – Bruno Santos Jan 09 '12 at 16:36
  • Is this problem has been resolved? Being injected with `@EJB` only? – Jin Kwon Jun 08 '12 at 09:26
  • Just a question for clarification - This works for you on JBoss71x within a plain ear/ejb-module structure? Could you maybe provide a simple working example? The whole Jax-RS (@Application, @Path) stuff seems to be ignored by my JBoss 7.1.1 – dwegener Apr 13 '13 at 10:31
0

Just add

@Stateless

TO the JAX-RS resources

@Path("/")
@Stateless
public class MainMain
{    

    @EJB(mappedName = "java:global/optima-wsi/OptimaConfigurator!com.sistemaits.optima.configurator.IOptimaConfigurator")
    IOptimaConfigurator configurator;

    ---
}
Tommaso
  • 520
  • 1
  • 6
  • 20
  • please see comments on previous post from CrazyPenguin about adding Stateless annotation to the JAX-RS resource:) – zdenda.online Mar 29 '12 at 09:30
  • That mappedName is horrible looking. Why did you felt the compulsion to use it? – Mike Braun Oct 21 '12 at 15:46
  • @Mike It dependes on where the code came from (an example on all the possibilities to do ejb injection), it's just an example, not a best practice :) – Tommaso Oct 22 '12 at 07:17