微服务架构和实践技术干货

Axis1.4调用天气预报WebService的API接口

2019-06-23  本文已影响0人  爱学习的蹭蹭

1、前言与目的

2、百度找一个天气预报wsdl或者其他进行测试

3、Eclipse生成WebService客户端代码步骤

鼠标右键选择new --->other -->Web Service Client


image.png

Web Service Client


image.png

Web Service 的WSDL访问连接设置


image.png

Web Service 生成代码的目录设置

image.png

Web Service Client生成代码

image.png

4、Axis需要的jar

<!-- asix begin -->
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>commons-discovery</groupId>
    <artifactId>commons-discovery</artifactId>
    <version>0.2</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis-jaxrpc</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis-saaj</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>wsdl4j</groupId>
    <artifactId>wsdl4j</artifactId>
    <version>1.4</version>
</dependency>
<!-- asix end -->

5、测试代码

public class TestWeather {

    public static void main(String[] args) throws ServiceException, RemoteException {
        WeatherWebServiceLocator locator = new WeatherWebServiceLocator();
        WeatherWebServiceSoapStub service = (WeatherWebServiceSoapStub) locator.getPort(WeatherWebServiceSoapStub.class);
        invokeGetSupportProvince(service);
        System.out.println("...................");
        invokeGetSupportCity(service);
        invokeGetWeatherByOneCity(service);
    }

    // 调用获取支持的省份、州接口
    public static void invokeGetSupportProvince(WeatherWebServiceSoapStub service) throws RemoteException {
        String[] provices = service.getSupportProvince();
        System.out.println("总共" + provices.length + "个");
        int count = 0;
        for (String str : provices) {
            if (0 != count && count % 5 == 0) {
                System.out.println();
            }
            System.out.print(str + "\t");
            count++;
        }
    }

    // 调用获取支持查询某个省份内的城市接口
    public static void invokeGetSupportCity(WeatherWebServiceSoapStub service) throws RemoteException {
        String provinceName = "江苏";
        String[] cities = service.getSupportCity(provinceName);
        System.out.println("总共" + cities.length + "个市");
        for (int i = 0; i < cities.length; i++) {
            if (0 != i && i % 5 == 0) {
                System.out.println();
            }
            System.out.print(cities[i] + "\t");
        }
    }

    // 调用查询某个城市天气的接口
    public static void invokeGetWeatherByOneCity(WeatherWebServiceSoapStub service) throws RemoteException {
        String cityName = "广州";
        String[] weatherInfo = service.getWeatherbyCityName(cityName);
        for (String str : weatherInfo) {
            System.out.println(str);
        }
    }
}

6、参考文章

7、工程源代码

工程源代码

上一篇 下一篇

猜你喜欢

热点阅读