前言
上一篇是cxf接口发布的方法,现在说说如何调用。我没有使用动态代理别的代理,我觉得既然都和spring整合了,就把注解进行到底。不过后面也贴上了wsdl2java的方式
开发环境:jdk7 、tomcat7、spring4.1.1 、hibernate4 、 cxf 3.1.5 等
客户端:
一、spring注解方式
1、添加cxf的jar包
同服务端所需的jar包是一样的,测试过了
2、拷贝接口类
是的,原封不动地拷贝过来就行了:
package com.yourpackage.webservice; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface YourService { @WebMethod public String yourMethod(yourParam...); }
3、整合spring配置客户端
<?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" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"> <jaxws:client id="iyourServiceClient" serviceClass="com.yourpackage.webservice.YourService" address='http://localhost:8080/yourProject/ws/YourService' /> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> <!-- 设置CXF的WebService客户端超时时长 --> <http-conf:conduit name="{http://webservice.yourpackage.com/}YourService.http-conduit"> <http-conf:client ConnectionTimeout="50000" ReceiveTimeout="100000"/> </http-conf:conduit> …… </beans>
4、调用接口
在需要使用接口的地方使用spring的bean注入,也可以手动注入,比如我是:
@Autowired private YourService server;
我还写了个工具类存放接口相关的东西,并注入了server,这样别的层次地方使用接口就不用依依注入,使用这个工具类就行了。然后就可以用server.YourMethod(...)
二、wsdl2java的方式(不推荐)
1、生成客户端调用类
打开命令窗口,cd到apache-cxf的bin目录下:
cd /d E:\apache-cxf-3.1.5\bin
输入:
wsdl2java -p com.yourpackage.webservice -d e:/src -client http://localhost:8080/yourProject/ws/YourService?wsdl
其中com.yourpackage.webservice是服务端接口类所在包路径,e:/src 是文件生成后放的位置
后面的地址就是接口的wsdl地址
执行命令后可以看到在E:\src\com\YourService\\webservice下有这些文件:
把src下的com文件夹整个拷贝到客户端,
也就是包路径和服务端YourService接口的包路径一致。
2、 获取接口服务
public class webserviceUtil { public static YourService service; static { YourWebService wa = new YourWebService (); service = wa.getYourServiceImplPort(); }
这种方式的优点是不用导入任何cxf的jar包,不用配置客户端xml文件,
缺点是如果接口里新增或修改方法就要重新生成wsdl2java文件,
而且在生成的类中,YourWebService 写死了wsdl的url,
如果接口路径或者部署的服务器变更的话,也要同步修改,
违背了代码易扩展易修改的原则。
至此接口调用完成了,关于webservice发布的方法请看上一篇:http://weilikk.iteye.com/admin/blogs/2317179