SpringBoot2.x系列教程80--SpringBoot整合Web Service获取当天的天气信息
作者:一一哥
我在上一节中,带大家学习了如何实现在SpringBoot中,利用Web Service实现RPC进程间通信。
接下来在本章节中,我会带大家实现在自己的项目中,获取别人第三方程序中的天气信息。
一. 第三方天气信息网站
1. 天气信息网站
我们可以去:
http://www.webxml.com.cn/zh_cn/index.aspx 这个网站上获取天气预报的服务接口,这个网站比较古老了,里面除了可以得到天气信息外,还可以获取手机号码归属地查询,车次查询等信息。

具体的地址是这个:
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx


所以我们接下来只要解析该WSDL服务接口就可以获取到天气信息了。
2. 安装配置apache-cxf
2.1 下载apache-cxf






二. 获取天气信息实现步骤
- 寻找Web Service服务获得wsdl;
- 根据wsdl生成接口;
- 编写接口程序;
- 将接口文件打包。
1. 获得wsdl
第三方网址:
http://www.webxml.com.cn/zh_cn/index.aspx
天气接口:
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx
2. 创建.wsdl文件
2.1 首先我们打开网址:
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl


2.2 创建一个文本文件
然后我们在任意目录下,创建一个文本文件:


2.3 复制xml文件内容
打开文件,把天气网站的所有文字复制粘贴到这个文件中。


复制到文本文件中。
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
...
...
...
</wsdl:definitions>
2.4 删除修改文本文件中的部分标签
接着,到这个文件中,Ctrl+F,把<s:element ref="s:schema"/>标签全部删除。

还要把 替换为&


2.5 将文本文件改成.wsdl文件


3. 利用命令根据wsdl文件生成接口
接下来我们利用之前安装的Apache CXF里的wsdl2java命令或者wsimport命令来生成获取天气信息的接口。
3.1 方式一:利用wsimport命令生成
在上面创建的目录下,在命令行中输入命令:
wsimport -keep weather.wsdl

就会生成一堆java文件。


注:
如果发生以下提示(wsimport 不是内部或外部命令,也不是可运行的程序或批处理文件).
解决方法,参考文章:
3.2 方式二:利用wsdl2java命令创建
在cmd命令行中运行wsdl2java命令。
wsdl2java F:\WeatherTarget\src\weather.wsdl
也可以生成同样的java文件。


4. 创建一个web项目


4.1 添加依赖包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.12</version>
</dependency>
4.2 把上面自动创建出的所有java类复制到项目中


4.3 把weather.wsdl文件复制到resource目录下


4.4 修改要加载的配置文件路径
将WeatherWS类中的一行代码:
url = new URL("file:weather.wsdl");
修改为:
url = new URL("classpath:/weather.wsdl");
否则运行时可能会导致FileNotFoundException。


5. 创建Controller类接口
package com.yyg.boot.weather.web;
import cn.com.webxml.ArrayOfString;
import cn.com.webxml.WeatherWS;
import cn.com.webxml.WeatherWSSoap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author 一一哥Sun
* @Date Created in 2020/5/10
* @Description Description
*/
@Slf4j
@RestController
public class WeatherController {
@GetMapping("/weather")
public List<String> getWeather() {
WeatherWSSoap weatherWSSoap = new WeatherWS().getWeatherWSSoap();
//获取国家信息
ArrayOfString arrayOfString = weatherWSSoap.getRegionCountry();
List<String> list = arrayOfString.getString();
for (String result : list) {
log.warn(result);
}
//获取省份
ArrayOfString arrayOfString1 = weatherWSSoap.getRegionProvince();
List<String> list1 = arrayOfString1.getString();
for (String s : list1) {
log.warn(s);
}
//获取城市-北京
ArrayOfString arrayOfString2 = weatherWSSoap.getSupportCityString("311101");
List<String> list2 = arrayOfString2.getString();
for (String s : list2) {
log.warn(s);
}
//注意:获取天气需要账号userid,每天可以免费试用5次,随便注册一个账号测试就可以。
ArrayOfString arrayOfString3 = weatherWSSoap.getWeather("上海", "自己的userid");
List<String> list3 = arrayOfString3.getString();
for (String s : list3) {
log.warn(s);
}
return list3;
}
}
如果没有输入用户userid,则会保存异常信息:


注意:
获取某个城市的天气信息时,需要获取账号userid,每天可以免费试用5次,随便注册一个账号测试就可以。
http://www.webxml.com.cn/zh_cn/index.aspx


注册登录该网站后,进入到后台,需要先开启试用功能,默认是没有开启的,否则即使注册了账号,输入了userid也无法获取到天气信息。


6. 创建入口类
package com.yyg.boot.weather;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author 一一哥Sun
* @Date Created in 2020/5/10
* @Description Description
*/
@SpringBootApplication
public class WeatherClientApplication {
public static void main(String[] args){
SpringApplication.run(WeatherClientApplication.class,args);
}
}
7. 启动项目测试
启动项目后,就可以看到我们已经成功的获取到了天气信息,只要适当的进行xml解析,把天气信息渲染到我们自己的网站上就可以了。


8.完整项目结构
