cxf 实战1

来源:互联网 发布:讲文明 懂礼仪 知荣辱 编辑:程序博客网 时间:2024/05/20 23:34

下面来看看HelloWorld的具体例子。

1.创建HelloWorld 接口类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.  
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.  
  8. @WebService 
  9. public interface HelloWorld {  
  10.     @WebMethod 
  11.     @WebResult String sayHi(@WebParam String text);  

2.创建HelloWorld实现类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.  
  3. public class HelloWorldImpl implements HelloWorld {  
  4.  
  5.     public String sayHi(String name) {  
  6.         String msg = "Hello " + name + "!";  
  7.         return msg;  
  8.     }  

3.创建Server端测试类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.  
  3. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  4.  
  5. // http://localhost:9000/HelloWorld?wsdl  
  6. public class Server {  
  7.     public static void main(String[] args) throws Exception {  
  8.         JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
  9.         factory.setServiceClass(HelloWorldImpl.class);  
  10.           
  11.         factory.setAddress("http://localhost:9000/ws/HelloWorld");  
  12.         factory.create();  
  13.  
  14.         System.out.println("Server start...");  
  15.         Thread.sleep(60 * 1000);  
  16.         System.out.println("Server exit...");  
  17.         System.exit(0);  
  18.     }  
  19. }  

4.创建Client端测试类

  1. package com.googlecode.garbagecan.cxfstudy.helloworld;  
  2.  
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.  
  5. public class Client {  
  6.     public static void main(String[] args) {  
  7.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  8.         factory.setServiceClass(HelloWorld.class);  
  9.         factory.setAddress("http://localhost:9000/ws/HelloWorld");  
  10.         HelloWorld helloworld = (HelloWorld) factory.create();  
  11.         System.out.println(helloworld.sayHi("kongxx"));  
  12.         System.exit(0);  
  13.     }  

5.测试

首先运行Server类来启动Web Service服务,然后访问http://localhost:9000/ws/HelloWorld?wsdl地址来确定web service启动正确。

运行Client测试类,会在命令行输出Hello kongxx!的message。

原创粉丝点击