在Web应用程序中从Web模块(RESTful服务)访问ejb [英] Access ejb from web module (RESTful services) in ear application

查看:209
本文介绍了在Web应用程序中从Web模块(RESTful服务)访问ejb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个耳机应用程序,由ejb.jar和Web应用程序(war)组成。
ejb.jar包含我的所有EJB(bean和接口),我的war包含REST Web服务。我想从战争模块访问EJB,是可能吗?注入EJB不起作用,我得到空指针异常。



我知道这已经被问了很多次,但我似乎无法让这个工作。



我正在使用Glassfish v2.1.1(我知道我应该升级,但现在很难...)



这是返回nullPointerException的代码片段:
(我知道我不应该通过GET验证用户,但这只是一个示例代码,我只是想检查我是否可以访问ebb

  @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 ,密码).toString();
}
}

我没有提到这段代码只能在我将ejb.jar包含在耳机应用程序的lib文件夹中,其中war模块可以看到它。有没有其他方式可以访问EJB类?
另外我正在使用Intellij,该应用程序是一个包含Web模块(用于战争)的Java EE应用程序。



编辑



我无法避免从v2到v4的glassfish升级。这是我做出的最好的决定,因为我的所有问题都消失了。我的代码中的所有ejb注入就像一个魅力!



感谢您的建议!我接受休息的答案,因为JNDI可能会工作,但升级玻璃鱼似乎是一个更合适的解决方案从长远来看。



编辑2



我终于设法使用这个代码:

  @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 ,密码).toString();
}
}

我在glassfish日志中找到了正确的查找路径我部署了我的耳机。

解决方案

该bean不会被注入,因为该类不是EJB。您应该可以使用JNDI查找获取副本:

  SB_usrRemote bean =(SB_usrRemote)new InitialContext()。lookup SB_usrRemote); 

有关JNDI查找的更多信息,请访问:链接



另一种可能性是使bean成为托管bean认为这是可能的JSF,但恐怕我不知道。


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.

解决方案

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]

这篇关于在Web应用程序中从Web模块(RESTful服务)访问ejb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆