欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Axis2生成Web Service的客户端 WebXPEclipseDOSJDK 

程序员文章站 2022-06-14 09:27:04
...
Axis2生成Web Service的客户端


用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方法如下:

    static public 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();
        }
    }

axis2 客户端调用三种方法
方法一:
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/axis2/services/AddressBookService");
options.setTo(targetEPR);
QName opAddEntry = new QName("http://service.addressbook.sample", "addEntry");
Object[] opAddEntryArgs = new Object[] { entry };
serviceClient.invokeRobust(opAddEntry, opAddEntryArgs);
方法二:
public static OMElement getEchoOMElement() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://example1.org/example1", "example1");
        OMElement method = fac.createOMElement("echo", omNs);
        OMElement value = fac.createOMElement("Text", omNs);
        value.addChild(fac.createOMText(value, "Axis2 Echo String "));
        method.addChild(value);
        return method;
    }
public static void main(String[] args) {
        try {
           OMElement payload = ClientUtil.getEchoOMElement();
            Options options = new Options();
          
            options.setTo(targetEPR);
            options.setAction("urn:echo");
            //Blocking invocation
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);
            OMElement result = sender.sendReceive(payload);
            System.out.println(result);
          
            System.out.println(options.getTo());
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }
方法三:
利用插件生成客户端
private static String URL="http://localhost:8080/axis2/services/WeatherService"; 
    public static void main(String[] args) throws RemoteException {
        try {
            WeatherServiceStub stub;
            if (args != null && args.length != 0) {
                stub = new WeatherServiceStub(args[0]);
              
            } else {
                stub = new WeatherServiceStub(URL);
            }
            WeatherServiceStub.SetWeather setWether = new WeatherServiceStub.SetWeather();
            WeatherServiceStub.Weather wether = new WeatherServiceStub.Weather();
          
            wether.setForecast("east");
            wether.setRain(true);
            wether.setTemperature(12.3f);
            wether.setForecast("big");
          
            setWether.setWeather(wether);
            stub.setWeather(setWether);
          
            WeatherServiceStub.GetWeatherResponse gw = new WeatherServiceStub.GetWeatherResponse();
            System.out.println(gw.get_return());       
          
        } catch (AxisFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }