如何在 Java Web 服务 SOAP 中注入 EJB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11579706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inject EJB in java web service SOAP
提问by Nikoas
i'm triyng to develop a simple web service in Java that used an EJB, with EJBs in jar file and WebServices in war file, both packaged in one ear. We use EjB 3.0 on JBOSS-AS 7.1.
我正在尝试用 Java 开发一个使用 EJB 的简单 Web 服务,其中 EJB 位于 jar 文件中,WebServices 位于 war 文件中,两者都打包在一只耳朵中。我们在 JBOSS-AS 7.1 上使用 EjB 3.0。
Here my EJB class with its interface :
这是我的 EJB 类及其接口:
@Stateless
public class TestEjb implements ITestEjb {
public boolean authentifier(String login, String password) {
if(login.equals("nico") && password.equals("nr"))
return true;
else
return false;
}
}
@Local
public interface ITestEjb {
public boolean authentifier(String login, String password);
}
And here is my web servicewhich inject the previous EJB:
这是我的Web 服务,它注入了以前的 EJB:
//@Stateless
@WebService
@SOAPBinding(style = Style.RPC)
public class AuthentificationWS {
@EJB
private ITestEjb testEjb;
public boolean authentifier(String login,String password) {
return testEjb.authentifier(login, password);
}
}
The deployment works well but when i call my web service, the EJB is always null(error : java.nullpointerException).
部署运行良好,但是当我调用 Web 服务时,EJB 始终为空(error : java.nullpointerException).
So i tried to add a @Stateless
to my web Service to convert it to EJB but without success when i try to consume it by a web service client.
因此,我尝试将 a 添加@Stateless
到我的 Web 服务以将其转换为 EJB,但是当我尝试通过 Web 服务客户端使用它时没有成功。
Then i tried to access Ejb by local jndi lookuplike below, taking the local jndi url in JBOSS log :
然后我尝试通过本地 jndi 查找访问 Ejb,如下所示,在 JBOSS 日志中获取本地 jndi url:
final String jndi = "java:module/TestEjb!fr.test.ITestEjb";
testEjb = (ITestEjb) new InitialContext().lookup(jndi);
But it did not work.
但它没有用。
Finally i tried with the global jndi urllike below :
最后,我尝试使用如下所示的全局 jndi url:
final String jndi = "ejb:Num-ear-1.0.0-SNAPSHOT/Num-ejb-1/TestEjb!fr.test.ITestEjb"
testEjb = (ITestEjb) new InitialContext().lookup(jndi);
And it works well !!
而且效果很好!!
So, i don't understand why my webservice cannot access locally to its EJB by injection or local Jndi way?
所以,我不明白为什么我的 web 服务无法通过注入或本地 Jndi 方式本地访问其 EJB?
回答by Duy Nguyen
The reason is because in JBOSS-AS 7.1, ejb service is deployed and mapped in different way. If you do not specify the mapped name for stateless bean, you will have to do look up using its global name, like this: ejb:Num-ear-1.0.0-SNAPSHOT/Num-ejb-1/TestEjb!fr.test.ITestEjb
原因是在 JBOSS-AS 7.1 中,ejb 服务的部署和映射方式不同。如果没有为无状态 bean 指定映射名称,则必须使用其全局名称进行查找,如下所示:ejb:Num-ear-1.0.0-SNAPSHOT/Num-ejb-1/TestEjb!fr.test .ITestEjb
You can try to add the mapped name by following:
您可以尝试通过以下方式添加映射名称:
@Stateless(name = "ejb/ar/TestEjb", mappedName = "ejb/ar/TestEjb")
public class TestEjb implements ITestEjb {
And in your webservice:
在您的网络服务中:
@EJB(name = "ejb/ar/TestEjb", mappedName = "ejb/ar/TestEjb")
private ITestEjb testEjb;
And in you web.xml:
在你的 web.xml 中:
<ejb-local-ref>
<ejb-ref-name>ejb/ar/TestEjb</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>yourpackage.TestEjb</local>
</ejb-local-ref>
In this case, I assume that you are deploying webservice as web module. For reference on how to deploy web service as web module, please take a look on this link: http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/
在这种情况下,我假设您将 Web 服务部署为 Web 模块。有关如何将 Web 服务部署为 Web 模块的参考,请查看此链接:http: //www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/
回答by Stelum
I used before: new InitialContext().lookup(java:app/domain/TestService) Converted to ear and used:
我之前使用过: new InitialContext().lookup(java:app/domain/TestService) 转换为ear并使用:
@Local
@Remote
public interface Test {}
@Stateless(name="Test", mapped-name="Test")
public TestService implements Test {}
@Webservice
public MyService{
@EJB(name="Test", mapped-name="Test")
private Test test;
}
This seems to work, but need to set @Remote in Test interface ():). Webservice is in war, domain in jar and both are contained in ear and deployed in Glassfish (3.1.2). It is a pity I had to use annotation @Remote because it is all in the same ear. If I omit the @Remote, I get exception at deploy time in Glassfish: "Trying to set an ejb-ref on an EJB, while the EJB does not define remote interfaces". Is there a way to omit the @Remote ??
这似乎有效,但需要在测试接口 ():) 中设置@Remote。Webservice 处于War状态,域在 jar 中,两者都包含在 ear 中并部署在 Glassfish (3.1.2) 中。很遗憾我不得不使用注释@Remote,因为它们都在同一个耳朵里。如果我省略@Remote,我会在 Glassfish 的部署时收到异常:“试图在 EJB 上设置 ejb-ref,而 EJB 没有定义远程接口”。有没有办法省略@Remote ?
回答by Johnathan Ingram
Try it without the interface, you don't need to define an interface in EJB3.x any longer
不带接口试试看,EJB3.x 不用再定义接口了
@Stateless
public class TestEjb {
public boolean authentifier(String login, String password) {
if(login.equals("nico") && password.equals("nr"))
return true;
else
return false;
}
}
Then inject into web service as follows
然后注入web服务如下
@WebService
public class AuthentificationWS {
@EJB
private TestEjb testEjb;
public boolean authentifier(String login,String password) {
return testEjb.authentifier(login, password);
}
}