mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-06-25 02:27:10 +08:00
Update RESTfulAPI简明教程.md
This commit is contained in:
parent
13820c2730
commit
49a3a152eb
@ -61,7 +61,7 @@ POST /classes:新建一个班级
|
||||
2. 客户端和服务器之间,传递这种资源的某种表现形式比如 `json`,`xml`,`image`,`txt` 等等;
|
||||
3. 客户端通过特定的 HTTP 动词,对服务器端资源进行操作,实现"表现层状态转化"。
|
||||
|
||||
## REST API 规范
|
||||
## RESTful API 规范
|
||||
|
||||

|
||||
|
||||
@ -77,13 +77,13 @@ POST /classes:新建一个班级
|
||||
|
||||
路径又称"终点"(endpoint),表示 API 的具体网址。实际开发中常见的规范如下:
|
||||
|
||||
1. **网址中不能有动词,只能有名词,API 中的名词也应该使用复数。** 因为 REST 中的资源往往和数据库中的表对应,而数据库中的表都是同种记录的"集合"(collection)。**如果 API 调用并不涉及资源(如计算,翻译等操作)的话,可以用动词。** 比如:`GET /calculate?param1=11¶m2=33`
|
||||
2. 不用大写字母,建议用中杠 - 不用下杠 \_ 比如邀请码写成 `invitation-code`而不是 ~~invitation_code~~
|
||||
1. **网址中不能有动词,只能有名词,API 中的名词也应该使用复数。**因为 REST 中的资源往往和数据库中的表对应,而数据库中的表都是同种记录的"集合"(collection)。如果 API 调用并不涉及资源(如计算,翻译等操作)的话,可以用动词。比如:`GET /calculate?param1=11¶m2=33` 。
|
||||
2. **不用大写字母,建议用中杠 - 不用下杠 \_** 。比如邀请码写成 `invitation-code`而不是 ~~invitation_code~~ 。
|
||||
3. **善用版本化 API**。当我们的 API 发生了重大改变而不兼容前期版本的时候,我们可以通过 URL 来实现版本化,比如 `Http://api.example.com/v1`、`http://apiv1.example.com` 。版本不必非要是数字,只是数字用的最多,日期、季节都可以作为版本标识符,项目团队达成共识就可。
|
||||
4. **接口尽量使用名词,避免使用动词。** RESTful API 操作(HTTP Method)的是资源(名词)而不是动作(动词)。
|
||||
|
||||
Talk is cheap!来举个实际的例子来说明一下吧!现在有这样一个 API 提供班级(class)的信息,还包括班级中的学生和教师的信息,则它的路径应该设计成下面这样。
|
||||
|
||||
**接口尽量使用名词,禁止使用动词。** 下面是一些例子:
|
||||
|
||||
```
|
||||
GET /classes:列出所有班级
|
||||
POST /classes:新建一个班级
|
||||
@ -132,14 +132,13 @@ GET /classes?page=1&size=10 //指定第1页,每页10个数据
|
||||
| | | 404 未找到 | |
|
||||
| | | 405 请求方法不对 | |
|
||||
|
||||
|
||||
## RESTful 的极致 HATEOAS
|
||||
|
||||
> **RESTful 的极致是 hateoas ,但是这个基本不会在实际项目中用到。**
|
||||
|
||||
上面是 RESTful API 最基本的东西,也是我们平时开发过程中最容易实践到的。实际上,RESTful API 最好做到 Hypermedia,即返回结果中提供链接,连向其他 API 方法,使得用户不查文档,也知道下一步应该做什么。
|
||||
|
||||
比如,当用户向 api.example.com 的根目录发出请求,会得到这样一个文档。
|
||||
比如,当用户向 `api.example.com` 的根目录发出请求,会得到这样一个返回结果
|
||||
|
||||
```javascript
|
||||
{"link": {
|
||||
@ -150,24 +149,26 @@ GET /classes?page=1&size=10 //指定第1页,每页10个数据
|
||||
}}
|
||||
```
|
||||
|
||||
上面代码表示,文档中有一个 link 属性,用户读取这个属性就知道下一步该调用什么 API 了。rel 表示这个 API 与当前网址的关系(collection 关系,并给出该 collection 的网址),href 表示 API 的路径,title 表示 API 的标题,type 表示返回类型 Hypermedia API 的设计被称为[HATEOAS](http://en.wikipedia.org/wiki/HATEOAS)。
|
||||
上面代码表示,文档中有一个 `link` 属性,用户读取这个属性就知道下一步该调用什么 API 了。`rel` 表示这个 API 与当前网址的关系(collection 关系,并给出该 collection 的网址),`href` 表示 API 的路径,title 表示 API 的标题,`type` 表示返回类型 `Hypermedia API` 的设计被称为[HATEOAS](http://en.wikipedia.org/wiki/HATEOAS)。
|
||||
|
||||
在 Spring 中有一个叫做 HATEOAS 的 API 库,通过它我们可以更轻松的创建除符合 HATEOAS 设计的 API。
|
||||
|
||||
## 文章推荐
|
||||
|
||||
**RESTful API 介绍:**
|
||||
|
||||
- [RESTful API Tutorial](https://RESTfulapi.net/)
|
||||
- [RESTful API 最佳指南](http://www.ruanyifeng.com/blog/2014/05/RESTful_api.html)(阮一峰,这篇文章大部分内容来源)
|
||||
- [[译] RESTful API 设计最佳实践](https://juejin.im/entry/59e460c951882542f578f2f0)
|
||||
- [那些年,我们一起误解过的 REST](https://segmentfault.com/a/1190000016313947)
|
||||
- [Testing RESTful Services in Java: Best Practices](https://phauer.com/2016/testing-RESTful-services-java-best-practices/)
|
||||
|
||||
**Spring 中使用 HATEOAS:**
|
||||
在 Spring 中有一个叫做 HATEOAS 的 API 库,通过它我们可以更轻松的创建除符合 HATEOAS 设计的 API。相关文章:
|
||||
|
||||
- [在 Spring Boot 中使用 HATEOAS](a)
|
||||
- [Building REST services with Spring](https://spring.io/guides/tutorials/classmarks/) (Spring 官网 )
|
||||
- [An Intro to Spring HATEOAS](https://www.baeldung.com/spring-hateoas-tutorial) (by [baeldung](https://www.baeldung.com/author/baeldung/))
|
||||
- [An Intro to Spring HATEOAS](https://www.baeldung.com/spring-hateoas-tutorial)
|
||||
- [spring-hateoas-examples](https://github.com/spring-projects/spring-hateoas-examples/tree/master/hypermedia)
|
||||
- [Spring HATEOAS](https://spring.io/projects/spring-hateoas#learn) (Spring 官网 )
|
||||
|
||||
## 参考
|
||||
|
||||
- https://RESTfulapi.net/
|
||||
|
||||
- http://www.ruanyifeng.com/blog/2014/05/RESTful_api.html
|
||||
|
||||
- https://juejin.im/entry/59e460c951882542f578f2f0
|
||||
|
||||
- https://phauer.com/2016/testing-RESTful-services-java-best-practices/
|
||||
|
||||
- https://www.seobility.net/en/wiki/REST_API
|
||||
|
||||
- https://dev.to/duomly/rest-api-vs-graphql-comparison-3j6g
|
Loading…
x
Reference in New Issue
Block a user