以下是一个通过spring JaxWsPortProxyFactoryBean调用wenservice接口的实例。这个到目前(2016-06-05)还可用的、用于查询天气的、免费的webservice接口地址是:http://www.webservicex.net/globalweather.asmx?wsdl
首先,我们需要用wsimport 生成客户端需要的java类:wsimport -d G:\tmp -keep -verbose http://www.webservicex.net/globalweather.asmx?wsdl ,然后我们将生成的java类拷贝到工程目录下。
applicationContext.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
default-autowire="byName">
<!-- 使用注解驱动特性 -->
<mvc:annotation-driven/>
<aop:aspectj-autoproxy />
<bean id="weatherService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean"
p:wsdlDocumentUrl="http://www.webservicex.net/globalweather.asmx?wsdl"
p:serviceName="GlobalWeather"
p:portName="GlobalWeatherSoap"
p:serviceInterface="net.webservicex.GlobalWeatherSoap"
p:namespaceUri="http://www.webserviceX.NET" />
</beans>
单元测试类:
package com.mytest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import net.webservicex.GlobalWeatherSoap;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:applicationContext*.xml"})
public class WeatherReportTest {
@Autowired
private GlobalWeatherSoap globalWeatherSoap;
@Test
public void test1(){
System.out.println(globalWeatherSoap.getCitiesByCountry("China"));
System.out.println(globalWeatherSoap.getWeather("Shenzhen", "china"));
}
}
返回如下:
<NewDataSet>
<Table>
<Country>China</Country>
<City>Beijing</City>
</Table>
<Table>
<Country>China</Country>
<City>Hohhot</City>
</Table>
<Table>
<Country>China</Country>
<City>Tianjin / Zhangguizhu</City>
</Table>
<Table>
<Country>China</Country>
<City>Taiyuan</City>
</Table>
<Table>
<Country>China</Country>
<City>Changsha</City>
</Table>
<Table>
<Country>China</Country>
<City>Guangzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Haikou</City>
</Table>
<Table>
<Country>China</Country>
<City>Guilin</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanning</City>
</Table>
<Table>
<Country>China</Country>
<City>Shantou</City>
</Table>
<Table>
<Country>China</Country>
<City>Shenzhen</City>
</Table>
<Table>
<Country>China</Country>
<City>Zhanjiang</City>
</Table>
<Table>
<Country>China</Country>
<City>Zhengzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Wuhan</City>
</Table>
<Table>
<Country>China</Country>
<City>Yinchuan</City>
</Table>
<Table>
<Country>China</Country>
<City>Jiuquan</City>
</Table>
<Table>
<Country>China</Country>
<City>Xi'An</City>
</Table>
<Table>
<Country>China</Country>
<City>Xining</City>
</Table>
<Table>
<Country>China</Country>
<City>Yan An</City>
</Table>
<Table>
<Country>China</Country>
<City>Kunming</City>
</Table>
<Table>
<Country>China</Country>
<City>Xiamen</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanchang</City>
</Table>
<Table>
<Country>China</Country>
<City>Fuzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Ganzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Hangzhou</City>
</Table>
<Table>
<Country>China</Country>
<City>Nanjing</City>
</Table>
<Table>
<Country>China</Country>
<City>Hefei</City>
</Table>
<Table>
<Country>China</Country>
<City>Qingdao</City>
</Table>
<Table>
<Country>China</Country>
<City>Shanghai / Hongqiao</City>
</Table>
<Table>
<Country>China</Country>
<City>Jinan</City>
</Table>
<Table>
<Country>China</Country>
<City>Chongqing</City>
</Table>
<Table>
<Country>China</Country>
<City>Guiyang</City>
</Table>
<Table>
<Country>China</Country>
<City>Lhasa</City>
</Table>
<Table>
<Country>China</Country>
<City>Chengdu</City>
</Table>
<Table>
<Country>China</Country>
<City>Hami</City>
</Table>
<Table>
<Country>China</Country>
<City>Kashi</City>
</Table>
<Table>
<Country>China</Country>
<City>Hotan</City>
</Table>
<Table>
<Country>China</Country>
<City>Urum-Qi / Diwopu</City>
</Table>
<Table>
<Country>China</Country>
<City>Yining</City>
</Table>
<Table>
<Country>China</Country>
<City>Changchun</City>
</Table>
<Table>
<Country>China</Country>
<City>Qiqihar</City>
</Table>
<Table>
<Country>China</Country>
<City>Dalian</City>
</Table>
</NewDataSet>
<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Shenzhen, China (ZGSZ) 22-33N 114-06E 18M</Location>
<Time>Jun 04, 2016 - 10:00 PM EDT / 2016.06.05 0200 UTC</Time>
<Wind> from the SSW (210 degrees) at 9 MPH (8 KT):0</Wind>
<Visibility> greater than 7 mile(s):0</Visibility>
<SkyConditions> mostly cloudy</SkyConditions>
<Temperature> 80 F (27 C)</Temperature>
<DewPoint> 77 F (25 C)</DewPoint>
<RelativeHumidity> 88%</RelativeHumidity>
<Pressure> 29.80 in. Hg (1009 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>
oschina git地址:https://git.oschina.net/empirecat/mywstest