Introduction

We know that REST is an architectural approach. It only specifies six basic principles that need to be followed, but the principles it specifies are relatively broad, and we need a more concrete constraint to guide our coding. This is HATEOAS.

Introduction to HATEOAS

The full English name of REST is REpresentational State Transfer, which means state transfer. The full name of HATEOAS is Hypertext As The Engine Of Application State, which means the state of using hypertext as an application. So the two are connected. HATEOAS specifies the manifestation of the state.

Hypertext is a link. Under the HATEOAS rules, all resource requests need to be accompanied by links. These links indicate the next step that can be performed on the resource. Moreover, these links are dynamically changing, depending on the requested resource. Therefore, if your architecture implements the HATEOAS style, you can continue to reduce the interface dependency between client and server. Because all the operations that can be performed have been placed in the hyperlinks that return resources.

Let's take an example, or the example of requesting students, if we request:

GET /students/zhangsan HTTP/1.1
Host: api.rest.com
Accept: application/json

Then the returned json may look like this:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: ...

{
    "student": {
        "student_id": 11111,
        "age": 10,
        "links": {
            "school": "/student/11111/school"
        }
    }
}

You can see that the returned information contains the student's own information and related links information, which contains the student's school information. The client can continue to get more information through the returned links.

If we visit another student, see what is the difference in the returned results:

GET /students/lisi HTTP/1.1
Host: api.rest.com
Accept: application/json

Then the returned json may look like this:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: ...

{
    "student": {
        "student_id": 2222,
        "age": 20,
        "links": {
            "school": "/student/2222/school",
            "vote": "/student/2222/vote",
        }
    }
}

Do you see any difference? This time the student’s age=20, so they have the right to vote, this time there is an additional vote link in our links.

Links will send changes according to different resources. The client does not need to know any server-side logic. Each request contains all the operations that can be continued, so that the client and the server are completely decoupled.

In the real world, when you visit a website, you click on its homepage. It provides some snapshots and links to other parts of the website. You click on them, and then you will get more information and more relevant links relevant to the context.

Similar to the interaction between humans and websites, the REST client accesses the initial API URI and uses the link provided by the server to dynamically discover available operations and access required resources. Customers do not need to know the different steps involved in the service or workflow in advance. In addition, the client no longer needs to hard-code the URI structure of various resources. HATEOAS allows the server to change the URI as the API develops without interrupting the client.

HATEOAS format

HATEOAS has two more important formats, RFC 5988 (web linking) and JSON Hypermedia API Language (HAL).

They are slightly different, but the principles are similar. Interested friends can check it out by themselves.

HATEOAS's Spring support

Whatever the people need, Spring will make it. Similarly, for this beautiful combination of REST+HATEOAS, how can Spring be missing?

Spring launched Spring HATEOAS to achieve this function. The latest version is 1.3.0. If you use Spring boot, it will be easier to use. Just quote the following XML:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
    <version>2.5.1</version>
</dependency>

If it is a non-Spring boot environment, you can quote it like this:

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
    <version>1.3.1</version>
</dependency>

In Spring HATEOAS, a series of very useful features are provided to help us create Link, thereby reducing our work. For the specific content of Spring HATEOAS, we will explain it in detail in a later article.

Summarize

If you use the REST architecture, then the HATEOAS rules should be the best combination. wish you success.

This article has been included in http://www.flydean.com/03-rest-hateoas/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com