如何使用JavaEE调用Web服务? [英] How to call a web-service using JavaEE?

查看:178
本文介绍了如何使用JavaEE调用Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用rpclib来自动生成一个WSDL并在Python中实现它。



然后我想调用一个具有此WSDL的Web服务*使用JavaEE,所以我简单地在Eclipse(Indigo 3.7.1与OEPE)中的创建向导中使用来自WSDL的 Web服务选项,但是后来的Ant构建失败了(简而言之):

  weblogic.wsee.tools.WsBuildException运行JAX-WS的错误wsdlc 
由java.lang.NoSuchMethodException引发的javax。 xml.bind.annotation.XmlElementRef.required()

我该怎么办?如何使用JavaEE调用Web服务?



* Web服务配置为:Apache HTTP Server 2.2.2 + mod_wsgi 3.3 + Python 2.6.5 + rpclib 2.6.1。

解决方案

好的,第二次偶然发现你的帖子,所以我会详细说明我之前给出的评论:)。 >

首先我重述你的设置:




  • 你有一个工作的webservice和一个URL指向相应的WSDL

  • 您将尝试从不同机器上的另一个Java EE项目调用WS方法。



调用WS的常规选项:


  1. 使用依赖注入注入WS参考

  2. 创建您的自己的WS存根

第一个选项将无法在您的设置中工作,因为DI只能在容器管理的环境中工作(见我的评论)。这意味着WS类和执行类必须在同一个容器中(例如同一个服务器)。



那么剩下的是手动生成你的WS stub 。因此,您可以使用您自己的答案中提到的 wsimport 工具。有几种不同的方法来使用这个工具。让我们来看看CLI的使用:


  1. 在您的IDE使用的WS客户端的projekt文件夹中导航:%IDE_WORKSPACE%/ your project / src

  2. 包装一个新的文件夹,例如 stub

  3. 在此目录中打开命令窗口

  4. 执行以下命令: wsimport -keep< http:// yourwsdl?wsdl>

  5. 刷新后,您应该会看到几个创建的文件

回到IDE中:



现在,您可以使用生成的存根文件通过从生成的服务获取端口连接到WS -file

  public class WsClient {

public static void main(String [] args){
//创建服务
' GeneratedFile'Service service = new'GeneratedFile'Service();

//创建代理
'GeneratedFile'proxy = service.get'GeneratedFile'Port();

//调用
System.out.println(proxy.yourMethod(yourParam));
}
}

最后提示:




  • 为了可移植性,请检查生成的文件。在其注释中,有时WSDL文件被链接到本地​​副本。只需将其更改回您的WSDL-URL。

    AFAIK有一个 wsimport 工具中的nofollow noreferrer>选项直接在导入例程中设置。

  • 有一个Eclipse插件称为 soapUI ,它允许您使用 wsimport 在Eclipse中的GUI中的工具。一旦设置,它应该加速你的工作。

  • 我还发现一个快速入门指南用eclipse开发WS客户端。



希望这有帮助,有



编辑:只是为了澄清:





使用 wsimport 工具,你应该有一个包含图像所示文件的目录。要清除此示例,您需要从 RequestFileService (这是我的WS操作)中获取服务,如 RequestFileService service = new RequestFileService(); 之后,您将需要此服务上的端口,如 RequestFile proxy = service.getRequestFilePort();

此后,您可以使用端口 proxy.yourMethod(yourParam);

I've been using rpclib to auto-generate a WSDL and implement it in Python.

Then I wanted to call a web-service* that has this WSDL using JavaEE, so I simply used the Web Service from WSDL option in the creation wizard in Eclipse (Indigo 3.7.1 with OEPE), but then the Ant build failed with the exception (in short):

weblogic.wsee.tools.WsBuildException Error running JAX-WS wsdlc
Caused by java.lang.NoSuchMethodException: javax.xml.bind.annotation.XmlElementRef.required()

What should I do? How can I call the web-service using JavaEE?

* The web service is configured with: Apache HTTP Server 2.2.2 + mod_wsgi 3.3 + Python 2.6.5 + rpclib 2.6.1.

解决方案

Ok, stumbled upon your post the second time, so I'll elaborate my comment given before :).

First I recapitulate your set-up:

  • You have a working webservice and an URL pointing to the corresponding WSDL
  • You'll try to invoke the WS methods from a different Java EE project on a different machine

General options for invoking a WS:

  1. Use Dependency Injection to inject the WS reference
  2. Create your own WS stubs

The first option won't work in your set-up because DI will only work in an container-managed-environment (see my comment). That means that the WS class and the executing class have to be in the same container (e.g. the same server).

So what is left is to generate your WS stubs manually. Therefore you can use the wsimport tool mentioned in your own answer. There are several different ways to use this tool. Lets have a look in the CLI use:

  1. navigate in your projekt folder of the WS client used by your IDE : %IDE_WORKSPACE%/your project/src
  2. crate a new folder, e.g. stub
  3. open a command window in this directory
  4. execute the follwing command : wsimport -keep <http://yourwsdl?wsdl>
  5. After a refresh you should see several created files

Back in your IDE:

Now you're able to use your generated stub-files to connect to the WS by getting a port from the generated service-file

public class WsClient {

  public static void main(String[] args) {
    //Create Service
    'GeneratedFile'Service service = new 'GeneratedFile'Service();

    //create proxy
    'GeneratedFile' proxy = service.get'GeneratedFile'Port();

    //invoke
    System.out.println(proxy.yourMethod(yourParam));
  }
}

Last hints:

  • For portabilty purpose check the generated files. In their annotations sometimes the WSDL file is linked to a local copy. Just change this back to your WSDL-URL.
    AFAIK there is an option in the wsimport tool to set this directly in the import routine.
  • There is a plugin for Eclipse called soapUI which allows you to use the wsimport tool in a GUI out of Eclipse. Once set up it should accelerate your work.
  • I've also found a quick start guide in developing WS clients with eclipse.

Hope this helped, have Fun!

EDIT: Just to clarify:

After you used the wsimport tool you should have a directory containing files like shown in the image. To make this example clear you'll need to get a Service from the RequestFileService (this is my WS operation) like RequestFileService service = new RequestFileService(); and after this you'll need a Port on this service like RequestFile proxy = service.getRequestFilePort();.
After this you can invoke your method calls by using the port proxy.yourMethod(yourParam);

这篇关于如何使用JavaEE调用Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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