java 对接webservice原来这么容易
之前写过一篇springboot对接webservice的文章https://blog.csdn.net/qq_15058425/article/details/103732273,发现我把对接webservice搞复杂了,而且我最近在对接新的webservice接口时,发现用上面这篇文章生成的代码及利用eclipse生成的代码是不一样的。而且利用eclipse生成的话更简单方便,如下我们开始尝试一下,例子还是上篇文章的查询天气的接口:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
1、现在eclipse中创建一个maven项目
2、利用eclipse生成代码
最后点击Finish即可
可以看到生成的代码:
3、写一个测试类测试一下:
public class Frist {
public static void main(String[] args) {
WeatherWebServiceSoapProxy service=new WeatherWebServiceSoapProxy();
try {
String[] provinces=service.getSupportProvince();
for(String province:provinces) {
System.out.println("支持的省份=="+province);
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出结果:
果然很简单吧
如下是pom配置,这个在webservice整合到项目的时候有用
<dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.5</version> </dependency>