通过spring JaxWsPortProxyFactoryBean调用webservice接口

以下是一个通过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








  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot可以使用JAX-WS或者Spring Web Services(Spring-WS)来调用SOAP Web Service接口,也可以使用RestTemplate来调用RESTful Web Service接口。 以下是使用Spring-WS调用SOAP Web Service接口的步骤: 1. 引入Spring-WS和JAXB相关依赖 ```xml <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置WebServiceTemplate 在配置类中添加WebServiceTemplate的Bean,并设置WebServiceTemplate的Marshaller和Unmarshaller,这里使用Jaxb2Marshaller ```java @Configuration public class WebServiceConfig { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("com.example.webservice.demo.wsdl"); return marshaller; } @Bean public WebServiceTemplate webServiceTemplate() { WebServiceTemplate template = new WebServiceTemplate(); template.setMarshaller(marshaller()); template.setUnmarshaller(marshaller()); template.setDefaultUri("http://localhost:8080/ws"); return template; } } ``` 3. 调用WebService 使用WebServiceTemplate的marshalSendAndReceive方法来发送SOAP请求并接收响应,示例代码如下: ```java @Autowired private WebServiceTemplate webServiceTemplate; public void callWebService() { GetCountryRequest request = new GetCountryRequest(); request.setName("Spain"); GetCountryResponse response = (GetCountryResponse) webServiceTemplate.marshalSendAndReceive(request); System.out.println(response.getCountry().getCapital()); } ``` 以上就是使用Spring-WS调用SOAP Web Service接口的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值