前一章是 CXF整合spring服务的发布,下面引用的服务来自于上一章节
一、引用服务
1.1新建工程
新建Dynamic Web Project动态工程,工程名:webservice_client_cxf_spring_1
1.2导入jar包
我用的版本是:apache-cxf-3.2.11.zip
可在官方进行下载,此处也给出百度云盘地址
官方地址:http://cxf.apache.org/download.html
百度云盘地址:https://pan.baidu.com/s/1ngJYeBf4ojeU6-f_dCMueA
提取码:7wzp
解压好后把apache-cxf-3.2.11/lib文件夹中所有的jar包复制在webservice_client_cxf_spring_1工程下lib目录中
1.3生成客户端代码
打开命令窗口,定位到webservice_client_cxf_spring_1下src目录
根据wsdl2java -d . http://localhost:8080/webservice_server_cxf_spring/ws/weatherService?wsdl
回车生成客户端代码
回到webservice_client_cxf_spring_1工程,刷新工程可看见生成的代码
1.4编写spring配置文件
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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="weatherService"
address="http://localhost:8080/webservice_server_cxf_spring/ws/weatherService"
serviceClass="com.qf.webservice.IWeatherService">
</jaxws:client>
</beans>
1.5调用方法
编写Client类,右击->Run As->Java Application直接运行
Client 类:
package com.qf.webservice.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qf.webservice.IWeatherService;
import com.qf.webservice.IWeatherServiceService;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IWeatherService bean = context.getBean(IWeatherService.class);
String query = bean.query("上海");
System.out.println(query);
}
}