REST接口定义

REST接口定义

REST即表述性状态传递(英文:representational state transfer),是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。

三种主流的web服务实现方案中,因为REST模式的web服务与复杂的SOAP和XML-RPC对比来讲明显更加简洁,越来越多的web服务开始采用REST风格设计和实现。

REST用来规范应用如何在HTTP层与API提供方进行数据交互 。REST描述了HTTP层里客户端和服务器端的数据交互规则;客户端通过向服务器端发送HTTP(s)请求,接收服务器的响应,完成一次HTTP交互。这个交互过程中,REST架构约定两个重要方面就是HTTP请求所采用的方法,以及请求的链接

REST规范可以抽象位两个规则:

  • 请求API的URL表示用来定位资源
  • 请求METHOD表示对这个资源的操作(注:定位资源与操作资源分开,则URL中不能有任何动词)
比如,我们有一个friends接口,对于“朋友”我们有增删改查四种操作,怎么定义REST接口?
增加一个朋友,uri: generalcode.cn/va/friends 接口类型:POST
删除一个朋友,uri: generalcode.cn/va/friends 接口类型:DELETE
修改一个朋友,uri: generalcode.cn/va/friends 接口类型:PUT
查找一个朋友,uri: generalcode.cn/va/friends 接口类型:GET

上面我们定义的四个接口就是符合REST协议的,这几个接口都没有动词,只有名词friends,都是通过HTTP请求的接口类型来判断是什么业务操作。定义这样一套统一的接口,在web,ios,android三端都可以使用相同的接口,十分方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用OpenAPI定义REST接口的示例: ``` openapi: 3.0.0 info: title: 示例API description: 用于演示OpenAPI定义REST接口的示例 version: 1.0.0 servers: - url: https://api.example.com/v1 paths: /users: get: summary: 获取用户列表 operationId: getUsers responses: '200': description: 成功获取用户列表 content: application/json: schema: type: array items: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 post: summary: 创建新用户 operationId: createUser requestBody: required: true content: application/json: schema: type: object properties: name: type: string description: 用户名 email: type: string description: 用户邮箱 required: - name - email responses: '201': description: 成功创建新用户 content: application/json: schema: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 /users/{userId}: get: summary: 获取指定用户信息 operationId: getUserById parameters: - name: userId in: path description: 用户ID required: true schema: type: integer responses: '200': description: 成功获取指定用户信息 content: application/json: schema: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 put: summary: 更新指定用户信息 operationId: updateUserById parameters: - name: userId in: path description: 用户ID required: true schema: type: integer requestBody: required: true content: application/json: schema: type: object properties: name: type: string description: 用户名 email: type: string description: 用户邮箱 required: - name - email responses: '200': description: 成功更新指定用户信息 content: application/json: schema: type: object properties: id: type: integer description: 用户ID name: type: string description: 用户名 email: type: string description: 用户邮箱 delete: summary: 删除指定用户 operationId: deleteUserById parameters: - name: userId in: path description: 用户ID required: true schema: type: integer responses: '204': description: 成功删除指定用户 ``` 以上是一个简单的示例,包含了获取用户列表、创建新用户、获取指定用户信息、更新指定用户信息和删除指定用户等5个接口。其中,每个接口都包含了请求参数、响应数据和状态码等信息,可以通过OpenAPI工具自动生成客户端或服务端代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值