Introduction to the

Microservices have been in full swing in recent years, with calls between microservices and a gradual shift from RPC calls to HTTP calls. So it’s common to hear some colleagues say that we provide micro-services and expose RESTful interfaces to other systems, but what is a RESTful interface? What does it have to do with REST? Don’t worry, this article will find out.

REST

REST is an architecture.

The first thing to remember is that REST is an architectural approach, not a protocol. It just tells us how to build a reliable system.

REST is the full name for Representational State Transfer. Chinese may not translate well, so we will define it as representative state escape for the time being. It is an architectural approach to distributed systems. It was first mentioned by Roy Fielding in his doctoral thesis in 2000.

REST architecture is very common in web applications today. It does not involve specific coding, it is just a high-level guidance scheme, the implementation of which is up to you.

REST and RESTful API

We’ve just covered REST, so what does REST have to do with RESTful APIs?

As we know, API is the bridge of communication between services and between clients and servers. Through the call between APIs, we can get the required resource information from the server. RESTful APIs are those that conform to the REST architecture.

So not all HTTP APIs are RESTful APIs, it’s based on the premise that your system is RESTful.

Fundamental principles of REST architecture

So what is a REST-architected system? According to Roy Fielding’s paper, there are six basic characteristics of a REST architecture system. Let’s explain as we go.

Uniform interface

In REST architecture, the most central element is the resource. We define resources as individual URIs. A resource is represented by a single and unique URI.

A single resource cannot be too large or too small; it represents a single operable unit. These resources are acquired and manipulated in a common way. For example, the CURD of a resource can be represented using different HTTP methods (PUT, POST, GET, DELETE).

At the same time, we need to name the resources uniformly and define the unified link format and data format.

One more thing, according to the HATEOAS protocol, a resource should also contain URIs that point to that resource or a related resource. Some of you are a little confused about this right now, but that’s OK, we’ll talk more about HATEOAS later.

Spring also provides support for HATEOAS. Let’s look at a basic HATEOAS request:

GET http://localhost:8080/greeting

The return of the request could look like this:

{ "content":"Hello, World!" , "_links":{ "self":{ "href":"http://localhost:8080/greeting? name=World" } } }

As you can see, it returns a link to a resource that represents its own URI.

Client — The server is independent of the Client

The other rule is that the client and server are separate, that the client and server don’t interact with each other, and that the only interaction between them is API calls.

For the client, as long as it can get the corresponding resources through the API, it doesn’t care how the server is implemented.

On the server side, the API remains the same, and the internal implementation is free to decide, regardless of how the client uses the API.

This rule has been used for many of today’s front-end separation architectures.

Stateless Stateless

Like the HTTP protocol, API calls between services in the REST architecture are stateless. Stateless means that the server does not keep a history of API calls, nor does it store any information about the client. Each request is up to date to the server.

Therefore, the user’s state information is saved and maintained on the client side. The client needs to put a unique mark on each interface that can identify the user, so as to conduct authentication and identification on the server side, so as to obtain the corresponding resources.

Cacheable Cacheable

Caching is a great way to speed up your system, and the same is true for REST resources, where a resource that is cacheable must be marked as cacheable.

Thus, the corresponding caller can cache these resources, thus improving the efficiency of the system.

Layered system

Modern systems are basically layered, and the same is true of REST architectures, which don’t care how many layers you use as long as you make sure that the resource URIs you provide are consistent.

Code on Demand

In general, services in a REST architecture interact with each other via JSON or XML. But it’s not a hard and fast rule. You can return executable code and run it directly.

Examples of RESTful APIs

Let’s take a few examples of common RESTful APIs to see the magic of this architecture:

Request an Entity:

GET https://services.odata.org/TripPinRESTierService/People

Request an entity based on the ID:

GET https://services.odata.org/TripPinRESTierService/People('russellwhyte')

Request a property of an entity:

GET https://services.odata.org/TripPinRESTierService/Airports('KSFO')/Name

Query with Filter:

GET https://services.odata.org/TripPinRESTierService/People?$filter=FirstName eq 'Scott'

Modify data:

POST https://services.odata.org/TripPinRESTierService/People
header:
{
    Content-Type: application/json
}
body:
{
    "UserName":"lewisblack",
    "FirstName":"Lewis",
    "LastName":"Black",
    "Emails":[
        "[email protected]"
    ],
    "AddressInfo": [
    {
      "Address": "187 Suffolk Ln.",
      "City": {
        "Name": "Boise",
        "CountryRegion": "United States",
        "Region": "ID"
      }
    }
    ]
}

Delete data:

DELETE https://services.odata.org/TripPinRESTierService/People('russellwhyte')

Update data:

PATCH https://services.odata.org/TripPinRESTierService/People('russellwhyte')
header:
{
    Content-Type: application/json
}
body:
{
    "FirstName": "Mirs",
    "LastName": "King"
}

conclusion

This article explains the concepts associated with REST and RESTful, so how do you define the most important resources? Stay tuned for more articles.

This article has been included in http://www.flydean.com/01-rest-restful/

The most popular interpretation, the most profound dry goods, the most concise tutorial, many you do not know the tips to wait for you to discover!