Season 1 link and series navigation: http://www.cnblogs.com/fzrain/p/3490137.html
Here, I would like to thank Taiseer Joudeh for bringing us the latest technology sharing. The landlord liked web api technology and received support from many friends, therefore, it is inevitable to continue. After the previous phase of study, we should be no stranger to web APIs. From this article, we will start to introduce OData. I have just started learning, so I will record the learning process and share it with you. I look forward to making progress together.
In this series, we will follow the project background of the first quarter to build an OData service. This article will use a simple example to help you understand OData, before introducing the example, we will first introduce the definition and conventions of OData.
OData Introduction
Open Data Protocol (OData) is a Web Protocol used to query and update Data. It provides a way to expose Data in applications. OData is used and built on many Web technologies, such as HTTP, Atom Publishing Protocol (AtomPub), and JSON. It provides the ability to access information from various applications, services, and repositories. OData is used to expose and access information from various data sources, including but not limited to relational databases, file systems, content management systems, and traditional Web sites.
We know that building services that comply with the REST principles should have the following features:
1. Each resource is identified by a unique URI.
2. Use the HTTP method (GET, POST, PUT, and DELETE) to implement CRUD for resources.
3. data can be returned in XML or JSON format specified by the client.
Operation
URL
Description
$ Filter Http://services.odata.org/Northwind/Northwind.svc/Products? $ Filter = ProductName eq 'tofu' Return results based on the Expression Status (return Products with ProductName equal to Tofu) $ Orderby Http://services.odata.org/Northwind/Northwind.svc/Products? $ Orderby = ProductName Sort by result (sort by ProductName column) $ Skip Http://services.odata.org/Northwind/Northwind.svc/Products? $ Skip = 10 N data entries in the result are crossed, which is often used in Paging $ Top Http://services.odata.org/Northwind/Northwind.svc/Products? $ Top = 10 The first n records in the returned results, which are commonly used in Paging $ Select Http://services.odata.org/Northwind/Northwind.svc/Products? $ Filter = ProductName eq 'tofu' & $ select = ProductName, UnitPrice Select the attribute to be returned $ Expand Http://services.odata.org/Northwind/Northwind.svc/Products? $ Expand = Supplier Returns the navigation property (associated property) Supplier contained in Products. $ Inlinecount Http://services.odata.org/Northwind/Northwind.svc/Products? $ Inlinecount = allpages Obtain the total number of qualified resources from the server (total by page)
Through the contents of the above table, we can also achieve complex queries by combining query conditions (for example: we want to achieve the server side paging, then we can use the following URI: http://services.odata.org/Northwind/Northwind.svc/Products? $ Top = 10 & $ skip = 0 & $ orderby = ProductName & $ inlinecount = allpages ). In the URI above, $ skip represents the skipped data, usually pageSize * pageIndex, and $ inlinecount returns an int value representing the total number of records.
The OData Service supports Content Selection. In other words, the client can customize the response data format by setting the request header Accept header. Each format has its own advantages and disadvantages. The following table describes the differences between the two formats:
|
XML |
JSON (details) |
JSON (lightweight Edition) |
OData version |
Versions 1, 2, 3 |
Versions 1, 2, 3 |
Version 3 |
Metadata and hyperlinks |
Including data and metadata |
Including data and metadata |
Contains only data |
Data size |
28.67 KBs |
14.34 KBs, nearly 50% smaller |
4.25 KBs, nearly 75% smaller |
Is the consumption of mobile clients simple? |
No |
Yes |
Yes |
Accept Header |
Application/atom + xml |
Application/json; odata = verbose |
Application/json |