使用cxf開發webservice接口


     

               項目中經常用到開發webservice接口,及調用webService接口。這里講解如何使用cxf開發webService接口。

一、webservice介紹及理解     

       webservice是一種跨平台,跨語言的規范,用於不同平台,不同語言開發的應用之間的交互。 
       比如,平台平台淘寶、京東想獲取其他快遞公司數據接口,需快遞公司開放數據接口。
       那么 webservice就是出於以上類似需求而定義出來的規范;無需關心對方什么平台上開發以及使用何種語言開發。
       只關心調用對方發布webservice接口的一些對我們獲取有用數據的方法。
       開發人員一般就是在具體平台開發webservice接口,以及調用webservice接口;每種開發語言都有自己的webservice實現框架。
       比如Java 就有 Apache Axis1、Apache Axis2、Codehaus XFire、Apache CXF、Apache Wink、Jboss  RESTEasyd等等...

        

 

二、cxf

  cxf是java開發webService的一種實現框架技術。目前,cxf是主流的webService實現框架。

      使用cxf開發需引入cxf開發相關jar包,maven項目中pom.xml配置如下:

     

 1 <!--添加cxf支持  -->
 2     <dependency>
 3         <groupId>org.apache.cxf</groupId>
 4         <artifactId>cxf-rt-frontend-jaxws</artifactId>
 5         <version>3.1.9</version>
 6     </dependency>
 7     <dependency>
 8         <groupId>org.apache.cxf</groupId>
 9         <artifactId>cxf-rt-transports-http-jetty</artifactId>
10         <version>3.1.9</version>
11     </dependency>
12     <dependency>
13         <groupId>org.apache.cxf</groupId>
14         <artifactId>cxf-core</artifactId>
15         <version>3.1.9</version>
16     </dependency>
View Code

  備注:這里要額外加入jetty,作為webservice發布的服務器。jetty是一個內嵌的web服務器;

  使用JaxWsServerFactoryBean類創建工廠設置暴露地址、接口類、接口實現類,創建即可發布。

 

三、下面演示其實現過程

      發布webService接口,需一個發布服務的url地址,及對應的接口。Jdk自身有實現WebService。

      具體實現代碼如下:

     根據規范,我們先建一個接口類:HelloWorld

     

 1 /**
 2  * 
 3  */
 4 package com.hik.webservice;
 5 
 6 import javax.jws.WebService;
 7 
 8 /**
 9  * @ClassName: HelloWorld
10  * @Description: TODO
11  * @author jed
12  * @date 2017年7月30日上午10:20:35
13  *
14  */
15 @WebService
16 public interface HelloWorld {
17 
18     public String say(String str);
19 }
View Code

    再建一個具體的實現類:HelloWorldImpl

 

 1 /**
 2  * 
 3  */
 4 package com.hik.webservice.impl;
 5 
 6 import javax.jws.WebService;
 7 
 8 import com.hik.webservice.HelloWorld;
 9 
10 /**
11  * @ClassName: HelloWorldImpl
12  * @Description: TODO
13  * @author jed
14  * @date 2017年7月30日上午10:24:46
15  *
16  */
17 @WebService
18 public class HelloWorldImpl implements HelloWorld{
19 
20     public String say(String str) {
21         return "hello"+str;
22     }
23 
24 }
View Code

 最后建一個發布服務的主類:Server

 1 /**
 2  * 
 3  */
 4 package com.hik.webservice;
 5 
 6 import javax.xml.ws.Endpoint;
 7 
 8 
 9 import com.hik.webservice.impl.HelloWorldImpl;
10 
11 /**
12  * @ClassName: Server
13  * @Description: TODO
14  * @author jed
15  * @date 2017年7月30日上午10:26:16
16  *
17  */
18 public class Server {
19     
20     public static void main(String[] args) {
21         System.out.println("web Service start");
22         HelloWorldImpl implementor = new HelloWorldImpl();
23         String address="http://192.168.0.102/helloWorld";
24         Endpoint.publish(address, implementor);//JDK實現
25         System.out.println("web Service started");
26         
27     }
28 }
View Code

這里的Endpoint是Jdk自身實現的WebService。這里的address,寫上自己的本機IP

我們運行下Server類:

運行效果如下:

 

 我們在瀏覽器里訪問:http://192.168.1.102/helloWorld?wsdl

 效果:

 

說明已經成功調用了webservice接口;

這里的wsdl 是 Web Services Description Language的縮寫,是一個用來描述Web服務和說明如何與Web服務通信的XML語言。WSDL是Web Service的描述語言,用於描述Web Service的服務,接口綁定等,為用戶提供詳細的接口說明書。

請求后得到的是一個xml規范文檔。是一套規范,任何語言平台技術都可以解析。

 

CXF來實現webservice接口

我們把Server改下。換成CXF實現:

 1 /**
 2  * 
 3  */
 4 package com.hik.webservice;
 5 
 6 import javax.xml.ws.Endpoint;
 7 
 8 import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
 9 
10 import com.hik.webservice.impl.HelloWorldImpl;
11 
12 /**
13  * @ClassName: Server
14  * @Description: TODO
15  * @author jed
16  * @date 2017年7月30日上午10:26:16
17  *
18  */
19 public class Server {
20     
21     public static void main(String[] args) {
22         System.out.println("web Service start");
23         HelloWorldImpl implementor = new HelloWorldImpl();
24         String address="http://192.168.0.102/helloWorld";
25         //Endpoint.publish(address, implementor);//JDK實現
26         JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
27         factoryBean.setAddress(address); //設置暴露地址
28         factoryBean.setServiceClass(HelloWorld.class); //接口類
29         factoryBean.setServiceBean(implementor); //設置實現類
30         factoryBean.create();
31         System.out.println("web Service started");
32         
33     }
34 }
View Code

效果和jdk實現的一樣

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM