ApacheCXF实战之一:HelloWorldWebService

Apache的CXF现在几乎成了Java领域构建Web Service的***类库,并且它也确实简单易用,下面就通过几篇系列文章做一下简单介绍。

当然首先想到的当然还是那个Hello World示例。这个系列文章中用到的例子都是基于Maven构建的工程,下面是我的pom.xml文件内容

 
 
 
  1.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  2.     4.0.0  
  3.     com.googlecode.garbagecan.cxfstudy  
  4.     cxfstudy  
  5.     war  
  6.     1.0-SNAPSHOT  
  7.     cxfstudy Maven Webapp  
  8.     http://maven.apache.org  
  9.       
  10.       
  11.         2.2.7  
  12.       
  13.       
  14.       
  15.           
  16.             org.apache.cxf  
  17.             cxf-rt-frontend-jaxws  
  18.             ${cxf.version}  
  19.           
  20.           
  21.             org.apache.cxf  
  22.             cxf-rt-transports-http  
  23.             ${cxf.version}  
  24.           
  25.           
  26.             org.apache.cxf  
  27.             cxf-rt-transports-http-jetty  
  28.             ${cxf.version}  
  29.           
  30.           
  31.             org.apache.cxf  
  32.             cxf-rt-ws-security  
  33.             ${cxf.version}  
  34.           
  35.           
  36.             org.apache.cxf  
  37.             cxf-rt-ws-policy  
  38.             ${cxf.version}  
  39.           
  40.           
  41.             org.apache.cxf  
  42.             cxf-bundle-jaxrs  
  43.             ${cxf.version}  
  44.           
  45.           
  46.             javax.ws.rs  
  47.             jsr311-api  
  48.             1.1.1  
  49.           
  50.           
  51.             org.slf4j  
  52.             slf4j-api  
  53.             1.5.8  
  54.           
  55.           
  56.             org.slf4j  
  57.             slf4j-jdk14  
  58.             1.5.8  
  59.           
  60.           
  61.             commons-httpclient  
  62.             commons-httpclient  
  63.             3.0  
  64.           
  65.           
  66.             commons-io  
  67.             commons-io  
  68.             1.4  
  69.           
  70.           
  71.             junit  
  72.             junit  
  73.             4.8.1  
  74.             test  
  75.           
  76.       
  77.       
  78.       
  79.         cxfstudy  
  80.           
  81.               
  82.                 src/main/resources  
  83.               
  84.               
  85.                 src/main/java  
  86.                   
  87.                     **  
  88.                   
  89.                   
  90.                     **/*.java  
  91.                   
  92.               
  93.           
  94.           
  95.               
  96.                 org.mortbay.jetty  
  97.                 maven-jetty-plugin  
  98.                   
  99.                     /  
  100.                       
  101.                           
  102.                             9000  
  103.                           
  104.                       
  105.                   
  106.               
  107.               
  108.                 org.apache.maven.plugins  
  109.                 maven-compiler-plugin  
  110.                   
  111.                     1.5  
  112.                     1.5  
  113.                   
  114.               
  115.           
  116.       
  117.  
  118.  

#p#

下面来看看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。

原文链接:http://blog.csdn.net/kongxx/article/details/7525476

【系列文章】

  1. Apache CXF实战之五:压缩Web Service数据
  2. Apache CXF实战之四:构建RESTful Web Service
  3. Apache CXF实战之三:传输Java对象
  4. Apache CXF实战之二:集成Sping与Web容器
  5. Apache CXF实战之一:Hello World Web Service

分享名称:ApacheCXF实战之一:HelloWorldWebService
分享路径:http://www.shufengxianlan.com/qtweb/news5/420405.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联