日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ApacheCXF實戰(zhàn)之一:HelloWorldWebService

Apache的CXF現(xiàn)在幾乎成了Java領域構建Web Service的***類庫,并且它也確實簡單易用,下面就通過幾篇系列文章做一下簡單介紹。

當然首先想到的當然還是那個Hello World示例。這個系列文章中用到的例子都是基于Maven構建的工程,下面是我的pom.xml文件內(nèi)容

 
 
 
  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.創(chuàng)建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.創(chuàng)建HelloWorld實現(xiàn)類

 
 
 
  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.創(chuàng)建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.創(chuàng)建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實戰(zhàn)之五:壓縮Web Service數(shù)據(jù)
  2. Apache CXF實戰(zhàn)之四:構建RESTful Web Service
  3. Apache CXF實戰(zhàn)之三:傳輸Java對象
  4. Apache CXF實戰(zhàn)之二:集成Sping與Web容器
  5. Apache CXF實戰(zhàn)之一:Hello World Web Service

分享標題:ApacheCXF實戰(zhàn)之一:HelloWorldWebService
文章起源:http://m.5511xx.com/article/ccsghes.html