oData


An open protocol to allow the creation and consumption of queryable and interoperable RESTful APIs in a simple and standard way.

開放數據協議(OData)是一個查詢和更新數據的Web協議。OData應用了web技術如HTTP、Atom發布協議(AtomPub)和JSON等來提供對不同應用程序,服務和存儲的信息訪問。除了提供一些基本的操作(像增刪改查),也提供了一些高級的操作類似過濾數據和實體的導航。OData擴展了上述的協議但是不是取代他們。他可以被XML(ATOM)或者JSON取代但是OData的重要在於它符合REST原則。在某種意義上,它建立在'簡單'的REST HTTP 服務上,並且有着清晰的目標——簡化和標准化我們操作和查詢數據的方式。如果你過去在給你的REST服務創建搜索、過濾、或者分頁API的時候感覺很麻煩,那么OData將是一個不錯的選擇。

接口調用說明:

下表列舉了一些常用的Odata操作:

操作

URL

說明

             $filter http://localhost:8090/api/Meetings?$filter=ProductName eq 'Tofu' 根據表達式的狀態返回結果(返回ProductName 等於Tofu的Products)
            $orderby http://localhost:8090/api/Meetings?$orderby=ProductName 根據結果排序(根據ProductName列排序)
            $skip http://localhost:8090/api/Meetings?$skip=10 越過結果中的n條數據,常用於分頁
            $top http://localhost:8090/api/Meetings?$top=10 返回結果中的前n條記錄,常用於分頁
           $select http://localhost:8090/api/Meetings?$filter=ProductName eq 'Tofu'&$select=ProductName,UnitPrice 選擇需要返回的屬性
           $expand http://localhost:8090/api/Meetings?$expand=Supplier 返回Products中包含的導航屬性(關聯屬性)Supplier
          $inlinecount http://localhost:8090/api/Meetings?$inlinecount=allpages 向服務器獲取符合條件的資源總數(分頁的total值)

通過上面表格的內容,我們還可以通過組合查詢條件來實現復雜的查詢。

常用查詢舉例:

  示例1:列出所有Product
  URL:http://localhost:8914/Products

   示例2,查詢Products,只列出Name,Price例
  URL:http://localhost:8914/Products?$select=Name,Price

  示例3:列出Products(只有列Name,Price),包括Supplier
  URL:http://localhost:8914/Products?$select=Name,Price&$expand=Supplier

  示例4:過濾Products,只顯示分類為Test的數據
  URL:http://localhost:8914/Products?$filter=Category eq ’Test‘

  示例5:過濾Products,只顯示分類為Test的數據,並排序
  URL:http://localhost:8914/Products?$filter=Category eq ’Test‘&$orderby=Price desc

  $filter的其它的使用方式:

  1.  http://localhost/Products?$filter=Category eq 'Test'
    過濾Category=Test
  2.http://localhost/Products?$filter=Price lt 10
    過濾Price小於10
  3.http://localhost/Products?$filter=Price ge 5 and Price le 15
    過濾5<=Price>=15
  4.還可以使用數據庫函數如:
    $filter=substringof('zz',Name)
    $filter=year(ReleaseDate) gt 2005

  5.關於排序:
    $orderby=Price
    $orderby=Price desc
    $orderby=Category,Price desc

  6.還有一些過濾器如:
    $skip,$top,$inlinecount等等


免責聲明!

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



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