独立的 Java Web 服务客户端

独立的 Java Web 服务客户端

问题描述:

总的来说,我是网络服务的新手.我正在尝试编写一个 Java 独立客户端,它可以从网络服务中获得响应.

I am new to webservices in general. I am trying to write a Java stand-alone client which can get a response back from a webservice.

我尝试搜索 SO 和 Google,但现在我更困惑了.以下是我广泛浏览的链接.

I tried searching SO and Google but now I got more confused. The below are the links I went through extensively.

我有一个类似的网址:http://api.something.com/remote/wsdl/SomeEncryptedText

我也有一个类似于以下内容的 SOAP 请求:

I also have a SOAP request something like:

<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
<soap12:Body> 
<AuthUsername>someName@someWhere.com</AuthUsername> 
<AuthPassword>mypassword</AuthPassword> 
<Sid>12121</Sid> 
<DynamicProductFeedsRequest xmlns="http://api.something.com/remote/SomeEncryptedText"> 
</DynamicProductFeedsRequest> 
</soap12:Body> 
</soap12:Envelope> 

有了这个,我如何编写一个独立的 Java 客户端,我想在稍后阶段将其与某些 Web 应用程序集成?

With this how do I write a stand-alone Java client which I would want to integrate with some web application at a later stage?

从前面提到的资源来看,有多种软件可供选择:SoapUI、WSDL2Java、Apache Axis、Maven 插件、JAX-WS、Apache CXF.

From the resources mentioned earlier looks there is a wide choice of softwares: SoapUI, WSDL2Java, Apache Axis, Maven Plugin, JAX-WS, Apache CXF.

我在其中使用了 http://www.soapclient.com/soaptest.html上面提到的 SO 答案中的一个,我能够在浏览器上获得一个完美的 html/xml 文件.

I used http://www.soapclient.com/soaptest.html in one of the SO answers mentioned above and I am able to get a perfect html/xml file on the browser.

现在我对我应该使用哪个软件感到困惑?链接中的信息很少,我无法相互关联,因为我对 SOA 一无所知.

Now I am confused on which is the software I should use? The information in the links are little in bits and pieces which I am unable to correlate with one another since I do not know anything in SOA.

谁能告诉我编写独立 Java 客户端的高级步骤,该客户端接收 WSDL URL 和 SOAP 请求并给我它的输出?

Could anyone please tell me the high level steps in writing a stand-alone Java client which takes in the WSDL URL and SOAP request and gives me the output of it?

如果我遗漏了任何信息,请告诉我.

Please let me know if I missed any information.

这个问题都取决于以下几点:

This question all depends on the following:

  • Java 编译器的 JDK 版本.
  • 您的 WSDL 版本(有 1.0、1.2 和 2.0).

基本上,如果您使用 Java 注释 要生成 Web 服务,那么您将需要 Java 5 相关的 Web 服务库(支持注解).

Basically, if you are using Java annotations to generate web services, then you'll need Java 5 related Web Services libraries (which supports annotations).

一些关于使用带有注解的 Java Web 服务 (JAX-WS) 的文章:

Some articles on Using Java Web Services with annotations (JAX-WS):

我将从使用支持注释的 Java 生成 Web 服务客户端开始.众所周知,生成 WSDL 到 Java 的客户端是 Apache Axis(最后一个版本是 22 年发布的 1.42006 年 4 月).这基本上采用 WSDL 定义并将其生成回客户端.它支持旧版本的 WSDL (1.0),如果您使用较新版本的 WSDL(1.2 和 2.0),它会崩溃.

I'll start from generating Web Service client with Java that doesn't support annotations. The well known client that generates WSDL to Java is Apache Axis (the last version is 1.4 released in 22 April 2006). This basically takes a WSDL definition and generates it back to client. It supports the old version of WSDL (1.0) and crashes if you use the newer versions of WSDL (1.2 and 2.0).

它的基本作用是获取您的 WSDL 并生成一个与您的 Web 服务通信的 Java 代理.它可以允许基于 RPC 和基于 XML 的通信.

What this basically does, it takes your WSDL and generates a java Proxy that communicates to your Web Service. It can allow RPC based as well as XML based communication.

对于支持注解的 Java,有两种有效的方法:

For Java that supports annotations there are, effectively, 2 ways of doing this:

  • 使用 Java 自己的 wsimport 命令(可执行文件位于 JDK_HOME/bin/ 文件夹下).
  • 使用 3rd Party 库,例如 Apache Axis 2(它有效地替代了 Apache Axis 并支持 WSDL2.0 版)或 Apache CXF(最高支持 WSDL 1.2).
  • Using Java's own wsimport command (the executable is found under the JDK_HOME/bin/ folder).
  • Using 3rd Party libaries such as Apache Axis 2 (which effectively replaces Apache Axis and supports WSDL version 2.0) or Apache CXF (which supports WSDL up to 1.2).

要使用 wsimport,您基本上需要转到 shell 命令(或编写脚本)并有效地执行此操作:

To use wsimport, you basically need to go to a shell command (or write a script) and effectively do something of this effect:

wsimport -d [outputdir] wsdl_file

并且您的 java 代理将在 [outputdir] 文件夹中找到.

and your java proxy will be found in the [outputdir] folder.

wsimport 是在 JDK 1.6 中找到的(不知道早期版本有没有).更多来源这里此处.

wsimport is found in JDK 1.6 (I don't know if it exists in earlier versions). More source here, and here.

对于 Apache Axis、Apache Axis 2 或 Apache CXF,有一个执行源代码生成的 WSDL2Java 类文件.

For Apache Axis, Apache Axis 2 or Apache CXF, there's a WSDL2Java class file that does source code generation.

这是关于如何在 WSDL2Java 的指南>Apache CXFApache Axis 2.

Here's a guide on how to use WSDL2Java in Apache CXF and in Apache Axis 2.

我希望这对您有所帮助,就像我花了 30 分钟的时间做这件事一样.:-)

I hope this helps you in some way as much as I spent like 30 minutes off work doing this. :-)