zdenda.online
zdenda.online

Reputation: 2514

Injecting EJB within JAX-RS resource on JBoss7

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());
    }

Upvotes: 3

Views: 4614

Answers (2)

Tommaso
Tommaso

Reputation: 524

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;

    ---
}

Upvotes: 0

Bruno Santos
Bruno Santos

Reputation: 1526

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();
    }

}

Upvotes: 2

Related Questions