webService-cxf(3)
3. 示例二:使用CXF开发
示例参考官网
3.1. CXF开发服务端
3.1.1. jar包准备
这个示例所用到的jar如下,当然实际开发中只要在官网中下载CXF,导入lib下的包即可(自己去除冲突),要注意CXF的版本中对JDK有要求,本示例的版本已经不支持Java 6,必须是Java 7以及以上。
3.1.2. 定义接口
package com.zhong.cxf.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* 示例接口
* @WebService,@WebMethod,@WebResult,@WebParam注解含义与jdk形式的注解一个意思
* @author zhonghai
*/
@WebService
publicinterface HelloService {
@WebMethod
@WebResult String hello(@WebParam(name="name") String name);
}
3.1.3. 接口实现
package com.zhong.cxf.service.impl;
import com.zhong.cxf.service.HelloService;
/**
* 示例实现
* 【这个实现类不需要做任何的标注】
* @author zhonghai
*/
publicclass HelloServiceImpl implements HelloService{
@Override
public String hello(String name) {
return"hello "+name;
}
}
3.1.3. 发布
package com.zhong.cxf.server;
importorg.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.zhong.cxf.service.HelloService;
importcom.zhong.cxf.service.impl.HelloServiceImpl;
/**
* 服务端基本代码
* @author zhonghai
*/
publicclass Server {
publicstaticvoidmain(String[] args) throws Exception{
HelloServiceImpl helloServiceImpl = new HelloServiceImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloService.class);
svrFactory.setAddress("http://localhost:9000/helloService");
svrFactory.setServiceBean(helloServiceImpl);
svrFactory.create();
System.out.println("cxf 服务端启动!");
}
}
3.2. 开发客户端
3.2.1. jdk的wsimort.exe工具生成客户端代码
wsimort.exe工具位于Jdk的bin目录下,执行命令:wsimport -keep url(url为wsdl文件的路径)生成客户端代码。
建一个客户端项目(普通的java项目):
在src下,运行wsimport -keep http://localhost:9000/helloService?wsdl
常用参数为:
-d<目录> - 将生成.class文件。默认参数。
-s<目录> - 将生成.java文件。
-p<生成的新包名> -将生成的类,放于指定的包下,自定义包结构。
注意:-s不能分开,-s后面有个小点,用于指定源代码生成的目录。点即当前目录。
如果使用了-s参数则会在目录下生成两份代码,一份为.class代码。一份为.java代码。
结果如下:
3.2.2. 使用CXF的wsdl2java工具生成
建一个客户端项目(普通的java项目):
本示例并没有配置CXF的环境,所以cmd里面命令执行时需要cd到wsdl2java所在目录。
附wsdl2java用法:
wsdl2java -p com -d D:\\src -all xx.wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,serviceproxy,,service interface, server mainline, client mainline, implementationobject, and an Ant build.xml file.
执行wsdl2java –d D:\xfC2\src –client http://localhost:9000/helloService?wsdl
3.2.3. 服务端方法调用
无论是用wsimort还是用wsdl2java均可使用下面的代码测试。
package com.zhong.cxf.client;
importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.zhong.cxf.service.HelloService;
import com.zhong.cxf.service.HelloServiceService;
/**
* 调用WebService的客户端
* @author zhonghai
*/
publicclass CxfCilent {
publicstaticvoid main(String[] args) {
//1、 利用jdk自带类访问服务端
//创建一个用于产生HelloService实例的工厂,
HelloServiceServicefactory = newHelloServiceService();
//通过工厂生成一个HelloService实例 HelloService hs =factory.getHelloServicePort();
//调用HelloService的hello方法
String resResult = hs.hello("zhong");
System.out.println("调用WebService的sayHello方法返回的结果是:"+resResult);
System.out.println("---------------------------------------------------");
//2、 利用cxf类访问服务端 测试用的包与service端的相同
JaxWsProxyFactoryBean factory2 = newJaxWsProxyFactoryBean();
factory2.setAddress("http://localhost:9000/helloService");
HelloService client = factory2.create(HelloService.class);
System.out.println(client.hello("zhongzhong"));
}
}
结果:
3.2.4. 使用eclipse的new web service
建一个客户端项目(普通的java项目):
右击项目选new —>other -> Web Service Client
填入服务地址http://localhost:9000/helloService?wsdl
点击 Next 后:
点击finish就完成了:
客户端代码测试示例:
package com.zhong.cxf.client;
importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.zhong.cxf.service.HelloService;
importcom.zhong.cxf.service.HelloServiceProxy;
/**
* 调用WebService的客户端
* @author zhonghai
*/
publicclass CxfCilent3 {
publicstaticvoid main(String[] args)throws Exception {
//1、 利用jdk自带类访问服务端
//创建一个用于产生HelloService实例的工厂
HelloServiceProxyfactory = newHelloServiceProxy();
//通过工厂生成一个HelloService实例
HelloService hs = factory.getHelloService();
//调用HelloService的hello方法
String resResult = hs.hello("zhong");
System.out.println("调用HelloService的hello方法返回的结果是:"+resResult);
System.out.println("---------------------------------------------------");
//2、 利用cxf类访问服务端
//【使用这种方式的前提是要针对自动生成的HelloService接口要加上与服务端相同的注解-_-||感觉好操蛋!】
JaxWsProxyFactoryBean factory2 = newJaxWsProxyFactoryBean();
factory2.setAddress("http://localhost:9000/helloService");
HelloService client = factory2.create(HelloService.class);
System.out.println(client.hello("zhongzhong"));
}
}
代码中的第二种方式利用cxf类 访问服务端 【使用这种方式的前提是要针对自动生成的HelloService接口要加上与服务端相同的注解-_-||感觉好操蛋!】
替换方式如下:
结果: