Web Service入門

jobbole發表於2012-10-16

  1. Web Service的定義

  目前對Web Service沒有統一的定義,定義一:Web Service是自包含的、模組化的應用程式,它可以在Web中被描述、釋出、查詢以及呼叫。定義二:Web Service是基於網路的、分散式的模組化元件,它執行特定的任務,遵守具體的技術規範,這些規範使得Web Service能與其他兼任的元件進行操作。定義三:所謂Web Service是指由企業釋出的完成其特別商務需求的線上應用服務,其他公司或應用軟體通過Internet來訪問並使用這項應用服務。筆者比較喜歡維基百科的定義:Web Service是一種面向服務的架構的技術,通過標準的Web協議提供服務,目的是保證不同平臺的應用服務可以互操作。

  目前對WebService還沒有絕對全面和準確的定義,但是一般Web Service通常包括:

  SOAP

  一個基於XML的可擴充套件訊息信封格式,需同時繫結一個傳輸協議。這個協議通常是HTTP或HTTPS、SMTP、XMPP。

  WSDL

  一個XML格式文件,用以描述服務埠訪問方式和使用協議的細節。通常用來輔助生成伺服器和客戶端程式碼及配置資訊。比如JAVA語言的wsdl2java 工具。

  UDDI

  一個用來發布和搜尋WEB服務的協議,應用程式可藉由此協議在設計或執行時找到目標WEB Service。

  2. 使用Web Service的方式

  遠端過程呼叫

  Web Service提供一個分散式函式或方法介面供使用者呼叫,這是一種比較傳統的方式。通常,在WSDL中對RPC介面進行定義。儘管最初的Web Service廣泛採用RPC這種方式部署,但是針對其過於緊密之耦合性的批評聲也絡繹不絕。原因是RPC式的Web Service服務是利用一個簡單的對映,把使用者請求直接轉換為一個特定語言編寫的函式或方法。

  面向服務架構

  相比RPC式的Web Service,面向服務架構(SOA)得到了大部分主要軟體供應商以及業界專家的支援和肯定。因為SOA方式更加關注如何去連線服務而不是去特地某個實現的細節。

  REST式服務

  表達性狀態轉移(Representational state transfer,REST)類似於HTTP協議,REST把介面限定在一組HTTP操作中,比如GTP、PUT、DELETE等以供呼叫,此種服務可以通過WSDL來描述SOAP訊息內容,通過HTTP限定動作介面;或者完全在SOAP中對動作進行抽象。

  3. HelloWorld

  筆者將使用Apache Cxf官網的How To示例來實現helloworld和後續的示例,並且筆者將使用maven工具來構建示例專案。關於cxf及maven請參考官網http://cxf.apache.orghttp://maven.apache.org。(示例使用maven構建專案pom.xml)

  編寫Web Service

  HelloWorld.java

@WebService
 public interface HelloWorld {
 String sayHi(@WebParam(name=”text”) String text); //這裡的text為輸入引數
 }

  HelloWorldImpl.java

package demo.hw.server;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import javax.jws.WebService;
 @WebService(endpointInterface = “demo.hw.server.HelloWorld”, serviceName = “HelloWorld”)
 public class HelloWorldImpl implements HelloWorld {
 public String sayHi(String text) {
 System.out.println(“sayHi called”);
 return “Hello ” + text;
 }
 }

  釋出服務

  Server.java

System.out.println(“Starting Server”);
 HelloWorldImpl implementor = new HelloWorldImpl();
 String address = “http://localhost:9000/helloWorld”; //釋出地址
 Endpoint.publish(address, implementor);

  訪問Web Service

  Client.java

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
 factory.getInInterceptors().add(new LoggingInInterceptor());
 factory.getOutInterceptors().add(new LoggingOutInterceptor());
 factory.setServiceClass(HelloWorld.class);
 factory.setAddress(“http://localhost:9000/helloWorld”); //地址為以上釋出的地址
 HelloWorld client = (HelloWorld) factory.create();
 String reply = client.sayHi(“HI”);
 System.out.println(“Server said: ” + reply);
 System.exit(0);

  3. XML與JSON

  兩者都是Web Service資源表示方式,直白點說,就是Web Service將以兩者之一來請求或者返回結果。它們定義如下:

  1. JSON 一種輕量級的資料交換格式,具有良好的可讀性和便於快速編寫的特性。另外,JSON採用的相容性很高的文字格式。

  2. XML 擴充套件標記語言,用於標記電子檔案使其具有結構性的標記語言,可以用來標記資料、定義資料型別,是一種允許使用者對自己的標記語言進行定義的源語言。XML、非常適合Web傳輸,其提供了一種統一方法來描述和交換獨立於應用程式或供應商的結構化資料。

  XML與JSON優缺點比較:

  1. 可讀性 兩者不相上下,JSON格式{property1:value1,property2:value2,…},XML格式value1value2。在描述上,XML要優於JSON。

  2. 可擴充套件性 這點XML可以做的,JSON也可以。

  3. 資料大小 這點JSON要優於XML,但是差別不是太大。

  4. 使用範圍 XML目前被業界廣泛採用,而javascript語言對JSON支援要比XML優越。

  5. 效能 經測試,JSON的解析速度幾乎是XML解析的10倍。

  4. CXF與Spring框架整合

  假定使用Maven來構建專案,pom.xml

  編寫Web Service

  HelloWorld.java

package demo.spring.service;
 import javax.jws.WebService;
 @WebService public interface HelloWorld {
 String sayHi(String text);
 }

  介面實現類HelloWorldImpl.java

package demo.spring.service;
 import javax.jws.WebService;
 @WebService(endpointInterface = “demo.spring.service.HelloWorld”)
 public class HelloWorldImpl implements HelloWorld {
 public String sayHi(String text) {
 System.out.println(“sayHi called”);
 return “Hello ” + text;
 }
 }

  在專案的WEB-INF目錄下新建一個”cxf-servlet.xml”檔案,內容:

  另外還需要設定Servlet。一般,Web專案中都會有一個Web.xml檔案,我們需要新增Spring ContextLoaderLister元素。當Web容器啟動時,將由Spring ContextLoaderLister來啟動Srping,並且載入配置檔案。這些配置檔案是通過context-param元素來定義的。

  …

contextConfigLocation WEB-INF/beans.xml //配置檔案
org.springframework.web.context.ContextLoaderListener

  最後在上述的beans.xml在建立客戶端。

  這裡可以選擇將”helloClient” 注入到Srping bean中,或者採用手動的方式從Spring application context中查詢:

ApplicationContext context = …; // your Spring ApplicationContext
 HellWorld client = (HelloWorld) context.getBean(“helloClient”);

  來自:http://blog.jobbole.com/29195/

相關文章