天天看點

使用Axis2建構REST Service

    本文講解REST服務的概念和如何使用Axis2建構REST 服務。

    REST是的<b>Representational State Transfer</b>縮寫,中文意思是表象化狀态轉換。維基百科上給出REST的宗旨是:REST 從資源的角度來觀察整個網絡,分布在各處的資源由URI确定,而用戶端的應用通過URI來擷取資源的表形。獲得這些表形緻使這些應用程式轉變了其狀态。随着不斷擷取資源的表形,用戶端應用不斷地在轉變着其狀态,所謂表形化的狀态轉變(Representational State Transfer)。

    REST是基于HTTP動詞和辨別資源的唯一的URI的,我們知道HTTP動詞有GET,POST,PUT和DELETE,這也就對應我們常說的CRUD(create, Read, Update and Delete)。

    POST:    create

    GET:    read

    PUT:    update

    DELETE: delete

    REST的要求:

用戶端和伺服器結構

連接配接協定具有無狀态性

能夠利用Cache機制增進性能

階層化的系統

Code On Demand - Javascript

    這裡實作一個使用Axis2建構REST服務例子。

Salary.java代碼:

package cn.edu.xidian;

public class Salary {

  public int getSalary(String name) {

    if ( name.equals("zhangsan") ) {

      return 3000;

    }

    else if ( name.equals("lisi") ) {

      return 4000;

    else 

      return 5000;

  }

}

    使用axis2 service archiver釋出成為web service,具體如何釋出,參考:使用Eclipse+Axis2建構Web Service應用(http://panpan.blog.51cto.com/489034/119204),服務名稱是SalaryService。

    在浏覽器輸入:http://localhost:8080/axis2/services/SalaryService/getSalary?name=zhangsan 

    即可看到服務傳回資訊:

&lt;ns:getSalaryResponse&gt;

&lt;ns:return&gt;3000&lt;/ns:return&gt;

&lt;/ns:getSalaryResponse&gt;

    寫用戶端代碼,調用剛釋出的SalaryService,代碼如下。

RESTClient.java代碼如下:

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.Constants;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import javax.xml.namespace.QName;

import javax.xml.stream.FactoryConfigurationError;

import javax.xml.stream.XMLOutputFactory;

import javax.xml.stream.XMLStreamException;

import javax.xml.stream.XMLStreamWriter;

public class RESTClient {

  private static String toEpr = "http://localhost:8080/axis2/services/SalaryService";

        public static void main(String[] args) throws AxisFault {

                Options options = new Options();

                options.setTo(new EndpointReference(toEpr));

                options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

                options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);

                ServiceClient sender = new ServiceClient();

                sender.setOptions(options);

                OMElement result = sender.sendReceive(getPayload());

                try {

                        XMLStreamWriter writer = XMLOutputFactory.newInstance()

                                        .createXMLStreamWriter(System.out);

                        result.serialize(writer);

                        writer.flush();

                } catch (XMLStreamException e) {

                        e.printStackTrace();

                } catch (FactoryConfigurationError e) {

                }

        }

        private static OMElement getPayload() {

                OMFactory fac = OMAbstractFactory.getOMFactory();

                OMNamespace omNs = fac.createOMNamespace(

                                "http://xidian.edu.cn", "xsd");

                OMElement method = fac.createOMElement("getSalary", omNs);

                OMElement value = fac.createOMElement("name", omNs);

                value.addChild(fac.createOMText(value, "lisi"));

                method.addChild(value);

                return method;

    運作這段代碼,結果如下:

&lt;ns:getSalaryResponse xmlns:ns="http://xidian.edu.cn"&gt;&lt;ns:return&gt;4000&lt;/ns:return&gt;&lt;/ns:getSalaryResponse&gt;

    注意:用戶端REST方式調用服務跟普通服務的唯一差別就是這一句話:

options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);設定REST調用方式。

    REST有以下優點:

可以利用緩存Cache來提高響應速度

通訊本身的無狀态性可以讓不同的伺服器的處理一系列請求中的不同請求,提高伺服器的擴充性

浏覽器即可作為用戶端,簡化軟體需求

不需要額外的資源發現機制

在軟體技術演進中的長期的相容性更好

    還要注意的是需要使用Axis2 1.4.1版本來釋出REST服務,但是本人經過測試,Axis21.3版本也可以成功。

<a href="http://down.51cto.com/data/2353563" target="_blank">附件:http://down.51cto.com/data/2353563</a>

     本文轉自panpan3210 51CTO部落格,原文連結:http://blog.51cto.com/panpan/186945,如需轉載請自行聯系原作者