1、创建一个maven项目,目录结构如下:
pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- <dependency>
<groupId>activation</groupId>
<artifactId>activation</artifactId>
<version>1.0.2</version>
</dependency>
-->
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.lucee</groupId>
<artifactId>axis-ant</artifactId>
<version>1.4.0.L0001</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
</project>
此处需要引用天气接口:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl,创建一个wsdl的文件将此处网址中的内容放入(删除wsdl中的a: href类似的注解)
根据wsdl生成客户端代码:
选中wsdl文件->右键->web services->Generate Client->next->fish生成客户端代码
测试代码如下:
package com.weather.services;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import cn.com.WebXml.WeatherWebServiceLocator;
import cn.com.WebXml.WeatherWebServiceSoapStub;
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);
}
}
}
运行生成如下: