WebService CXF学习(入门篇2):HelloWorld示例

 理论联系实际,单单只讲理论那就成了纸上谈兵,用一个HelloWorld Demo可来说明事更加直观。那下面咱们就开始进行讲解: 
   首先到apache官方网下载apache-cxf-2.2.2,(现在有更高版本2.4)地址:http://cxf.apache.org/ 
   新建一个Java Project,导入cxf常用.jar包,cxf常用jar包如下:

1、commons-logging-1.1.1.jar
2、cxf-2.4.1.jar
3、geronimo-activation_1.1_spec-1.1.jar
4、geronimo-annotation_1.0_spec-1.1.1.jar
5、geronimo-javamail_1.4_spec-1.7.1.jar
6、geronimo-jaxws_2.2_spec-1.0.jar
7、geronimo-servlet_3.0_spec-1.0.jar
8、geronimo-stax-api_1.0_spec-1.0.1.jar
9、geronimo-ws-metadata_2.0_spec-1.1.3.jar
10、jettison-1.3.jar
11、jetty-continuation-7.4.2.v20110526.jar
12、jetty-http-7.4.2.v20110526.jar
13、jetty-io-7.4.2.v20110526.jar
14、jetty-server-7.4.2.v20110526.jar
15、jetty-util-7.4.2.v20110526.jar
16、neethi-3.0.0.jar
17、saaj-api-1.3.jar
18、saaj-impl-1.3.2.jar
19、serializer-2.7.1.jar
cxf结合spring时所需jar包
(
    spring-asm-3.0.5.RELEASE.jar
    spring-beans-3.0.5.RELEASE.jar
    spring-context-3.0.5.RELEASE.jar
    spring-core-3.0.5.RELEASE.jar
    spring-expression-3.0.5.RELEASE.jar
    spring-aop-3.0.5.RELEASE.jar
    spring-web-3.0.5.RELEASE.jar
)
20、wsdl4j-1.6.2.jar
21、xalan-2.7.1.jar
22、xercesImpl.jar
23、xml-resolver-1.2.jar
24、xmlschema-core-2.0.jar
25、jaxb-api-2.2.1.jar  ---- webservices服务端需要加
26、jaxb-impl-2.2.1.1.jar ---- 如果jdk中的版本与该版本一致,则webservices服务端和客户端都不需要加
(注:jaxb-api和jaxb-impl包会和jdk中的冲突(jdk的版本<2.0>较低),在用这两个jar包的时候,可将2.2版本的jar覆盖jdk的版本)
覆盖jdk版本的方法: 找到jdk的安装目录下的jre\lib\endorsed文件夹(如果endorsed文件夹不存在,可新建),将jaxb-api-2.2.1和jaxb-impl-2.2.1.1放到此文件夹下即可。

第一步:新建一个webservice服务端接口和实现类

 1、服务端接口

[java]  view plain copy
  1. package com.ws.services;  
  2. import javax.jws.WebService;  
  3.   
  4. @WebService  
  5. public interface IHelloServices {  
  6.     public String sayHelloToAll(String[] userNames);  
  7.   
  8.     public String[] getHelloWords();  
  9.       
  10.     public String sayHello(String name);  
  11. }  

  2、服务端接口实现类

[java]  view plain copy
  1. package com.ws.services.impl;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.ws.services.IHelloServices;  
  6.   
  7. @WebService(endpointInterface="com.ws.services.IHelloServices")  
  8. public class HelloServicesImpl implements IHelloServices {  
  9.   
  10.     public String[] getHelloWords() {  
  11.         String[] words = {"hello vicky.","hello,i'm vicky.","hi,ivy and simon."};  
  12.         return words;  
  13.     }  
  14.   
  15.     public String sayHello(String name) {  
  16.         return "hello "+name+" ! ";  
  17.     }  
  18.   
  19.     public String sayHelloToAll(String[] userNames) {  
  20.         String hello = "hello ";  
  21.         for(int i=0;i<userNames.length;i++){  
  22.             if(i!=userNames.length-1)  
  23.                 hello += userNames[i]+" and ";  
  24.             else  
  25.                 hello += userNames[i]+" .";  
  26.         }  
  27.         return hello;  
  28.     }  
  29.   
  30. }  

 3、创建webservices服务端,并发布服务

[java]  view plain copy
  1. package com.test;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  5. import com.ws.services.IHelloServices;  
  6. import com.ws.services.impl.HelloServicesImpl;  
  7. import com.ws.services.impl.UserServicesImpl;  
  8.   
  9. public class ServerTest {  
  10.     public ServerTest(){  
  11.         // 第一种发布方式  
  12.         IHelloServices hello = new HelloServicesImpl();  
  13.         // 创建WebServices服务接口  
  14.         JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
  15.         // 注册webservices接口  
  16.         factory.setServiceClass(IHelloServices.class);  
  17.         // 发布接口  
  18.         factory.setAddress("http://localhost:8090/helloServices");  
  19.         factory.setServiceBean(hello);  
  20.         // 创建服务  
  21.         factory.create();  
  22.           
  23.         // 第二种发布方式  
  24.         //Endpoint.publish("http://localhost:8090/helloServices", new HelloServicesImpl());  
  25.     }  
  26.     public static void main(String[] args) {  
  27.         // 启动服务  
  28.         new ServerTest();  
  29.         System.out.println("Server ready...");     
  30.         try {  
  31.             Thread.sleep(1000*300); //休眠五分分钟,便于测试     
  32.         } catch (InterruptedException e) {  
  33.             e.printStackTrace();  
  34.         }     
  35.         System.out.println("Server exit...");     
  36.         System.exit(0);  
  37.     }  
  38. }  


 

第二步:新建一个webservice客户端,并测试webServices的服务

    1、在本工程中测试(即服务端与客户端在同一个工程中)

[java]  view plain copy
  1. package com.ws.client;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4. import com.ws.server.IHelloServices;  
  5.   
  6. public class HelloTest {  
  7.     public static void main(String[] args) {  
  8.         //创建WebService客户端代理工厂     
  9.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();     
  10.         //注册WebService接口     
  11.         factory.setServiceClass(IHelloServices.class);     
  12.         //设置WebService地址     
  13.         factory.setAddress("http://localhost:8090/helloServices");          
  14.         IHelloServices iHelloWorld = (IHelloServices)factory.create();     
  15.         System.out.println("invoke webservice...");     
  16.         String[] hellos = iHelloWorld.getHelloWords();  
  17.         for(int i=0;i<hellos.length;i++){  
  18.             System.out.println(hellos[i]);  
  19.         }  
  20.         System.exit(0);     
  21.     }    
  22. }  


     2、新建一个webservices客户端测试工程

     (1)、新建一个Project,并加上cxf的jar包;

     (2)、将Webservices服务端工程中的接口类Copy到客户端工程中,且路径要一直;

     (3)、新建一个测试类,代码如上。

最后是万事俱备,只欠测试了 
    首先,运行服务端程序 
    其次,打开浏览器,在地址栏中输入http://localhost:8090/helloServices?wsdl(因为cxf自带了一个jetty服务器),查看接口是否发布成功,如里浏览器页面显示下面内容,证明接口发布成功 。
    最后,运行客户程序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值