---
title: "使用Axis2创建一个Web Service的客户端"
author: "lee_xiaodong"
date: "2010-04-02T17:48:06Z"
tags: []
categories: ["编程语言"]
slug: "axis2-web-service"
draft: false
lastmod: "2010-04-02T17:48:06Z"
id: 631070
url: "https://lixd.iteye.com/blog/631070"
---

本文应用Axis2的客户端自动生成功能，生成了一个客户端。

具体操作如下（Window XP）：

1、下载解压缩axis2。前提是已经安装好了jdk并设置好了JAVA_HOME系统变量。

2、添加 AXIS2_HOME系统变量，同时把bin目录放在path中。

3、到提供web service的网址下在wsdl，我使用的例子是http://www.webservicex.net/globalweather.asmx?WSDL，这里要注意，因为我们可以看到，每行前面都有一个短线，所以需要我们查看源代码，然后把源代码保存为weather.wsdl。

4、然后在工程目录下，运行以下DOS命令
>WSDL2Java -uri resources/weatherservice.wsdl -p src -d xmlbeans -s -o build/client

这是我们就可以看到Axis2生成了很多文件和文件夹，这里就不一一介绍，有兴趣地可以自己研究一下。

为了测试生成的结果，我们继续以下的步骤，而在开发过程中，就应该根据自己的实际项目，进行处理。

5、在刚才声称目录的文件夹中建立Eclipse工程。把Axis2的库引入到项目中。这时，应该项目中没有错误。

6、把resources目录包含的项目的类目录中。

7、找到GlobalWeatherStub类，在其中添加main方法如下：

```java
public static void main(String[] args)...{
        try...{
            GlobalWeatherStub sws = new GlobalWeatherStub();
            GetCitiesByCountryDocument gcbcd = GetCitiesByCountryDocument.Factory.newInstance();
            gcbcd.addNewGetCitiesByCountry();
            gcbcd.getGetCitiesByCountry().setCountryName("china");
            GetCitiesByCountryResponseDocument response = sws.GetCitiesByCountry(gcbcd);
            System.out.println(response.getGetCitiesByCountryResponse().getGetCitiesByCountryResult());
        }catch(AxisFault af)...{
            af.printStackTrace();
        }catch(RemoteException re)...{
            re.printStackTrace();
        }
    } 
```



运行GlobalWeatherStub类，我们就可以看到Eclipse在控制板中打印出来的城市列表了。
本文来自CSDN博客：http://blog.csdn.net/idilent/archive/2007/10/22/1836694.aspx