Injecting EJB within JAX-RS resource on JBoss7

天涯浪子 提交于 2020-01-02 07:46:06

问题


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

回答1:


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

}



回答2:


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;

    ---
}


来源:https://stackoverflow.com/questions/8166708/injecting-ejb-within-jax-rs-resource-on-jboss7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!