標籤: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技術學習與交流” 部落格,謝絕轉載!