什么是 REST 资源? [英] What are REST resources?

查看:30
本文介绍了什么是 REST 资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 REST 资源?它们与资源名称和资源表示有何关系?

我读了几篇关于这个主题的文章,但它们太抽象了,让我比以前更加困惑.

以下网址是资源吗?如果是,该资源的名称是什么?它的表示是什么?

<块引用>

http://api.example.com/users.json?长度=2&偏移=5

URL 的 GET 响应应该类似于:

<预><代码>[{编号:6,姓名:约翰"},{编号:7,姓名:简"}]

解决方案

什么是资源?

资源是任何重要到足以被引用为事情本身.如果您的用户可能想要创建超文本链接对它,作出或反驳关于它的断言,检索或缓存一个它的表示,通过引用将其全部或部分包含在另一种表示,对其进行注释或对其执行其他操作它",那么你应该把它变成一种资源.

通常,资源是可以存储在计算机上的东西表示为比特流:文档、数据库中的一行或运行算法的结果.资源可能是物理的像苹果这样的对象,或者像勇气这样的抽象概念,但是(如我们稍后会看到)这些资源的表示必然是令人失望.以下是一些可能的资源:

  • 软件版本 1.0.3 版
  • 最新版本的软件发布
  • 2006 年 10 月 24 日的第一个博客条目
  • 阿肯色州小石城的路线图
  • 关于水母的一些信息
  • 与水母有关的资源目录
  • 1024 之后的下一个质数
  • 1024 之后的五个质数
  • Q42004 的销售数据
  • Alice 和 Bob 两个熟人之间的关系
  • bug 数据库中未解决的 bug 列表

文字来自 O'Reilly 的书RESTful Web 服务".

What are REST resources and how do they relate to resource names and resource representations?

I read a few articles on the subject, but they were too abstract and they left me more confused than I was before.

Is the following URL a resource? If it is, what is the name of that resource and what is its representation?

http://api.example.com/users.json?length=2&offset=5

The GET response of the URL should look something like:

[
   {
      id: 6,
      name: "John"
   },
   {
      id: 7,
      name: "Jane"
   }
]

解决方案

What’s a Resource?

A resource is anything that’s important enough to be referenced as a thing in itself. If your users might "want to create a hypertext link to it, make or refute assertions about it, retrieve or cache a representation of it, include all or part of it by reference into another representation, annotate it, or perform other operations on it", then you should make it a resource.

Usually, a resource is something that can be stored on a computer and represented as a stream of bits: a document, a row in a database, or the result of running an algorithm. A resource may be a physical object like an apple, or an abstract concept like courage, but (as we’ll see later) the representations of such resources are bound to be disappointing. Here are some possible resources:

  • Version 1.0.3 of the software release
  • The latest version of the software release
  • The first weblog entry for October 24, 2006
  • A road map of Little Rock, Arkansas
  • Some information about jellyfish
  • A directory of resources pertaining to jellyfish
  • The next prime number after 1024
  • The next five prime numbers after 1024
  • The sales numbers for Q42004
  • The relationship between two acquaintances, Alice and Bob
  • A list of the open bugs in the bug database

The text is from the O'Reilly book "RESTful Web Services".

这篇关于什么是 REST 资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆