java 如何从 EJB 使用 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1276842/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:54:37  来源:igfitidea点击:

How to consume a webservice from an EJB

javaweb-servicesdependency-injectionjakarta-eeejb-3.0

提问by Daniel Schneller

I am currently trying to wrap my mind around Java EE 5. What I'd like to do is create a sample application that

我目前正在尝试围绕 Java EE 5 进行思考。我想做的是创建一个示例应用程序

  • offers a simple stateless EJB (e. g. a simple calulator with an add() method)
  • expose this add method as a webservice
  • consume this webservice from another EJB
  • 提供一个简单的无状态 EJB(例如一个带有 add() 方法的简单计算器)
  • 将此添加方法公开为网络服务
  • 从另一个 EJB 使用此 Web 服务

The first two steps are easy and I can deploy and test this bean to Glassfish v2.1 already and test it with a standalone client:

前两个步骤很简单,我可以将这个 bean 部署和测试到 Glassfish v2.1,并使用独立客户端进行测试:

@WebService
@Stateless
public class CalculatorWS {

    @WebMethod
    public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
        int k = i + j;
        return k;
    }
}

What I do not get however, is how to consume a webservice like this from a second EJB. While not strictly useful in this example, I will have to write some EJBs soon that will wrap external webservices as to keep my internal clients from having to deal with those external resources.

然而,我不明白的是如何从第二个 EJB 使用这样的网络服务。虽然在这个例子中并不是非常有用,但我很快将不得不编写一些 EJB 来包装外部 Web 服务,以使我的内部客户端不必处理这些外部资源。

From what I understand, I should be able to have the container inject me a webservice into a field of my EJB? I did not find an example for this, however. I'd welcome any hints to tutorials covering this - or even better an example right here :-)

据我了解,我应该能够让容器将 Web 服务注入我的 EJB 字段中吗?但是,我没有找到这方面的示例。我欢迎任何有关涵盖此内容的教程的提示 - 或者甚至更好的示例 :-)

For what it's worth, I am using Eclipse 3.5.

就其价值而言,我使用的是 Eclipse 3.5。

采纳答案by kazanaki

From the official Java EE tutorial

来自官方 Java EE 教程

Consuming a Web service

使用 Web 服务

回答by Mykola Golubyev

I would just inject this Calculator Bean to another stateless Bean with @EJB. Just make sure your bean implements some interface to be able to inject.

我只是将这个 Calculator Bean 注入到另一个带有@EJB 的无状态 Bean 中。只要确保你的 bean 实现了一些能够注入的接口。

回答by djna

The Web Service client isn't really EJB specific. So I think you'd use JAX-WS client techniques, your EJB environment being a managed envirnment (JNDI etc all nicely available).

Web 服务客户端并不是真正特定于 EJB。所以我认为你会使用 JAX-WS 客户端技术,你的 EJB 环境是一个托管环境(JNDI 等都很好用)。

I know you're not using WebSphere but I hope the techniques explained hereare generally applicable.

我知道您没有使用 WebSphere,但我希望这里解释的技术是普遍适用的。