Apache CXF 是一个开源的 Services 框架

Apache CXF 是一个开源的 Services 框架

下载 apache-cxf-2.2.10 ,apache-cxf-2.2.10-src

 

 

1:new Java Project


2:apache-cxf-2.2.10\apache-cxf-2.2.10\lib中的jar 导入项目

 

3: 创建接口

 

Java代码    收藏代码
  1. package com.apache.cxf.demo;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. @WebService //标注最终被XFire读取后进行分析后会进一步处理成Web服务  
  6. public interface CxfDemoInterFace {  
  7.   
  8.     public String sayHelloWord(String name);  
  9. }  

 

实现类 package com.apache.cxf.demo;

Java代码    收藏代码
  1. import javax.jws.WebService;  
  2.   
  3. @WebService  
  4. public class CxfDemoImpl implements CxfDemoInterFace{  
  5.   
  6.     @Override  
  7.     public String sayHelloWord(String name) {  
  8.         // TODO Auto-generated method stub  
  9.         System.out.println("Say HelloWord");  
  10.         return "Hello "+ name;  
  11.     }  
  12.   
  13. }  

 


 4:创建WebService的启动类

 

Java代码    收藏代码
  1. package javacode.apacheCxf.demo;  
  2.   
  3. import org.apache.cxf.endpoint.Server;  
  4. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  5.   
  6. /** 
  7.  * 服务器 
  8.  * @author playjava 
  9.  * 
  10.  */  
  11. public class StartWebServer {  
  12.   
  13.     public static void main(String[] args) {  
  14.         JaxWsServerFactoryBean jwf = new JaxWsServerFactoryBean();  
  15.         jwf.setServiceClass(CxfDemoImpl.class); //设置webservice的具体实现类  
  16.         jwf.setAddress("http://localhost:8080/CxfDemo");    //设置地址  
  17.         Server server = jwf.create();  
  18.         server.start();  
  19.           
  20.     }  
  21.   
  22. }  

 执行Main方法控制台输出显示:

 

成功启动代码    收藏代码
  1. 信息: jetty-6.1.21  
  2. 2010-10-10 13:36:43 org.mortbay.log.Slf4jLog info  
  3. 信息: Started SelectChannelConnector@localhost:8080  

地址栏访问设置路径:http://localhost:8080/CxfDemo显示如下

 



 表示 WebServer启动成功

 

 5:创建客户端

 

Java代码    收藏代码
  1. package javacode.apacheCxf.demo;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.   
  5. /** 
  6.  * 客户端 
  7.  * @author playjava 
  8.  * 
  9.  */  
  10. public class Client {  
  11.   
  12.     public static void main(String[] args) {  
  13.         JaxWsProxyFactoryBean jwpf = new JaxWsProxyFactoryBean();  
  14.         jwpf.setAddress("http://localhost:8080/CxfDemo");  
  15.         jwpf.setServiceClass(CxfDemo.class);  
  16.         CxfDemo cxfd = (CxfDemo)jwpf.create();  
  17.         System.out.println(cxfd.sayHelloWord("JE"));  
  18.     }  
  19. }   

执行Main方法客户端控制台输出显示: 

 

Java代码    收藏代码
  1. Hello JE  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值