使用CXF開發Web Service服務

來源:互聯網
上載者:User

標籤:web service

1、使用CXF開發Web Service服務端

1.1 開發一個Web Service業務介面,該介面要用@WebService修飾

(1)建立一個Java項目MyServer

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/40/wKiom1PlBUfC3RlPAAJcLKClvY8527.jpg" title="1.jpg" alt="wKiom1PlBUfC3RlPAAJcLKClvY8527.jpg" />

(2)在MyServer項目中建立一個介面HelloWorld

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/40/wKiom1PlBZDDXUNMAAGfkb4skIs760.jpg" title="2.jpg" alt="wKiom1PlBZDDXUNMAAGfkb4skIs760.jpg" />

package com.xju.ws;import javax.jws.WebService;@WebServicepublic interface HelloWorld {String sayHello(String name);}


1.2 開發一個Web Service實作類別,實作類別也需要用@WebService修飾

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/41/wKioL1PlB8PjrWuVAAJNdUJh04A915.jpg" title="3.jpg" alt="wKioL1PlB8PjrWuVAAJNdUJh04A915.jpg" />

package com.xju.ws.impl;import javax.jws.WebService;import com.xju.ws.HelloWorld;@WebService(endpointInterface = "com.xju.ws.HelloWorld", serviceName = "HelloWorldWs")public class HelloWorldWs implements HelloWorld {@Overridepublic String sayHello(String name) {// TODO Auto-generated method stubreturn null;}}


1.3 使用Endpoint類的靜態方法來發布WebService

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/45/41/wKioL1PlCe3giuerAAI1hhjTTHQ775.jpg" title="4.jpg" alt="wKioL1PlCe3giuerAAI1hhjTTHQ775.jpg" />

package com.xju.ws.pub;import javax.xml.ws.Endpoint;import com.xju.ws.HelloWorld;import com.xju.ws.impl.HelloWorldWs;public class ServerMain {public static void main(String[] args) {HelloWorld hw=new HelloWorldWs();Endpoint.publish("http://127.0.0.1:8080/test", hw);System.out.println("發布成功");}}

備忘:在運行中必須添加CXF2.7運行庫。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/45/41/wKioL1PlDvnzBA2KAAP9ic6yLr8488.jpg" title="6.jpg" alt="wKioL1PlDvnzBA2KAAP9ic6yLr8488.jpg" />


2 使用CXF開發Web Service用戶端

2.1 調用CXF提供的wsdl2java工具,根據WSDL檔案產生相應的Java代碼

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/45/41/wKioL1PlDIvTYZLXAAJehfQhFjQ549.jpg" title="5.jpg" alt="wKioL1PlDIvTYZLXAAJehfQhFjQ549.jpg" />


2.2 找到wsdl2java所產生類中,一個繼承了Service的類,該類的執行個體當成工廠來使用



2.3 調用Service子類的執行個體的getXxxPort方法,返回遠程WebService代理




本文出自 “IT技術學習與交流” 部落格,謝絕轉載!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.