ceph-rest-api_HATEOAS-REST API的重要概念

ceph-rest-api

HATEOAS stands for Hypertext As The Engine Of Application State. Now what does it mean and why this is important. It's a well agreed belief that a good written software is the one which has a good documentation. But there is little deviation in this belief when it comes to the Rest API. A Rest API is said to be perfect if it doesn't need any documentation at all. It should be so crystal clear to the developer who is consuming the service that he/she should not even refer to the documentation. It should be designed like a website, like once we go to the Home page we can navigate to the different resources with the help of hyperlinks.

HATEOAS代表超文本作为应用程序状态的引擎。 现在这意味着什么,为什么这很重要。 人们普遍认为,好的书面软件就是拥有良好文档的软件。 但是就Rest API而言,这种信念几乎没有偏差。 如果完全不需要任何文档,那么据说Rest API是完美的。 对于正在使用该服务的开发人员来说,应该非常清楚,以至于他/她甚至不应该参考文档。 它应该像一个网站一样设计,就像一旦进入主页我们就可以在超链接的帮助下导航到其他资源一样。

Now the question is how to achieve this? The simple answer is providing as much related links as possible about the resource in the response. Lets take the case of our registration application.

现在的问题是如何实现这一目标? 简单的答案是在响应中提供尽可能多的有关资源的相关链接。 让我们以我们的注册申请为例。

The typical response of the students resource /myApp/students/1:

学生资源/myApp/students/1的典型响应:

<Student>
    <rollno>10</rollno>
    <firstName>Amit</firstName >
    <lastName>Agarwal</lastName>
    <age>25</age>
</Student>

What if we give the additional information to the client, about how to check all the courses that this student (with the given roll no.) has enrolled into.

如果我们向客户提供有关如何检查该学生(具有给定卷号)已注册的所有课程的其他信息,该怎么办。

<Student>
    <rollno>10</rollno>
    <firstName>Amit</firstName >
    <lastName>Agarwal</lastName>
    <age>25</age>
    <link rel = “courses” href =”/myApp/students/1/registrations”>
</Student>

Now this will be very useful to the client as he will directly get the courses enrolled resource URI information from the students response itself. This is a HATEAOS concept.

现在,这对客户将非常有用,因为他将直接从学生响应本身获取课程注册的资源URI信息。 这是HATEAOS的概念。

We can also include more options like, URI to Update student information, Delete the student data etc.

我们还可以包括更多选项,例如URI以更新学生信息,删除学生数据等。

Similarly for the courses URI: /myApp/courses/1234

同样,对于课程URI: /myApp/courses/1234

<Courses>
    <CourseID>1234</ CourseID>
    <CourseName>REST</ CourseName>
    <CourseFess>10000</ CourseFess>
    <courseDuration>30</CourseDuration>
    <courseStartDate>25-12-2014</courseStartdate> 
</Courses>

Now once, a client has the course information, he/she might want to know about the students who have enrolled for the course, we can send the link of the resource URI of the students enrolled in this course.

现在,一旦客户获得了课程信息,他/她可能想要了解已注册该课程的学生,我们可以发送该课程已注册学生的资源URI的链接。

<Courses>
    <CourseID>1234</ CourseID>
    <CourseName>REST</ CourseName>
    <CourseFess>10000</ CourseFess>
    <courseDuration>30</CourseDuration>
    <courseStartDate>25-12-2014</courseStartdate>
    <link rel=”students” href=”/myApp/courses/1234/registrations”> 
</Courses>

So HATEOAS is a concept to provide links of the related sub resources to the resource which is requested by the client so that is becomes easier for the client to make further calls to the REST API.

因此,HATEOAS是一种概念,用于提供相关子资源到客户端所请求资源的链接,从而使客户端更容易对REST API进行进一步的调用。

We will learn in the coming lessons how to achieve this but keep this in mind this is an important design aspect and should be implemented very carefully.

在接下来的课程中,我们将学习如何实现此目标,但请记住这是重要的设计方面,应非常小心地实施。

So always remember,

所以永远记住

A GOOD REST API DOES NOT NEED ANY DOCUMENTATION
良好的REST API不需要任何文档

翻译自: https://www.studytonight.com/rest-web-service/hateoas

ceph-rest-api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值