简体   繁体   中英

Access ejb from web module (RESTful services) in ear application

I have an ear application that consists of an ejb.jar and a web application (war). The ejb.jar contains all my EJBs (beans and interfaces) and my war contains REST web services. I want to access the EJBs from the war module, is that possible? Injecting the EJBs doesn't work, I get null pointer exception.

I know that this has been asked many times but I can't seem to get this working...

I am using Glassfish v2.1.1 (I know I should upgrade, but right now it is difficult...)

Here is the code fragment that returns nullPointerException: (I know I am not supposed to validate a user through GET, but this is just an example code I am trying just to check if I can access the ejb)

@Path("/user")
public class UserWS
{
    @EJB
    SB_usrRemote sb_usrRemote;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String checkLoginAttempt(String username, String password)
    {
        return sb_usrRemote.checkLoginAttempt(username, password).toString();
    }
}

I didn't mention that this code only works when I include the ejb.jar in the ear application's lib folder where the war module can "see" it. Is there any other way I can access the EJB classes? Also I am using Intellij, the application is a java EE Application that includes a web module (for the war).

EDIT

I couldn't avoid the glassfish upgrade from v2 to v4. It was the best decision I made as all my problems went away. All the ejb injection in my code worked like a charm!

Thank you both for your suggestions! I am accepting hugh 's answer because JNDI probably would have worked, but upgrading glassfish seemed like a more appropriate solution in the long run.

EDIT 2

I finally managed to do a proper ejb injection using this code:

@Path("/user")
public class UserWS
{
    @EJB (lookup = "java:global/<ear-name>/<ejb-jar-name>/SB_usrRemote")
    SB_usrRemote sb_usrRemote;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String checkLoginAttempt(String username, String password)
    {
        return sb_usrRemote.checkLoginAttempt(username, password).toString();
    }
}

I found the proper lookup path in the glassfish log when I deployed my ear file.

Per the JAX-RS specification, injection is not enabled by default in JAX-RS classes. You either need to use a CDI bean deployment archive (add WEB-INF/beans.xml for CDI 1.0), make the JAX-RS class be a managed bean (add the @ManagedBean annotation to the class)., or make the JAX-RS class be an EJB (eg, add the @Stateless annotation to the class).

The bean isn't getting injected because this class isn't an EJB. You should be able to get a copy using a JNDI lookup:

SB_usrRemote bean = (SB_usrRemote) new InitialContext().lookup("SB_usrRemote");

More on JNDI lookups here: link

Another possibility is to make the bean a managed bean - I think that's possible in JSF, but I'm afraid I don't know about that. [Edit - JAX-RS, not JSF, as per Bkail's answer]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM