1、前言与目的
-
Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序
-
此文章主要通过最新版本的Eclipse 2019.3版本的自带WebService Client生成在线的wsdl接口的代码进行调用.
-
由于公司对接外部系统WebService协议,然而根据公司业务的需要场景整理一个工程Demo出来提供实际学习案例与积累。
2、百度找一个天气预报wsdl或者其他进行测试
-
在百度输入"天气预报wsdl" 点击第一个
image.png
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);
}
}
}

image.png
image.png
image.png









网友评论