• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

营造宁静的方式表决定的资源

用户头像
it1352
帮助1

问题说明

我需要知道如何决定什么是资源,什么是子资源。我只是有一些表的,我需要做的URI。因此可以说,我有一个学生表,studentclass(这给名字,可能一些学生在就读于类的更多细节)表(N:1)。

I need to know how to decide what is a resource and what is a sub resource. I just have some tables for which i need to make uri's. so lets say i have a student table and studentclass(which gives the name and probably some more details of the class that the student is enrolled in) table with (N:1).

我会让学生就像一个主资源和设计URI低于

I will make student a main resource and design uri like below

http://home/someapi/v1/student/1/

随着建筑URI的休息方法

Following a rest approach for building uri

QUS 1:哪些如下所示的URI做我必须做的访问studentclass

Qus 1: Which uri shown below do i have to make for accessing the studentclass

1.http://home/someapi/v1/studentclass?studentid=1 or
2.http://home/someapi/v1/student/1/studentclass

我要考虑studentclass作为一个单独的资源..?

Do i have to consider studentclass as a separate resource..?

QUS 2:假设有N:3 N表之间的关系则URI的设计应如何

Qus 2: Suppose there is N:N relation between tables then how should the design of uri be?

正确答案

#1

我建议处理两个资源:学生

I suggest to deal with two resources: student and class.

您可以使用以下URI获得学生

You can get a student using the following URI:

http://home/someapi/v1/students/1

Note the plural here, read this article if you want to learn why you should use plural: http://blog.apigee.com/detail/restful_api_design_plural_nouns_and_concrete_names/.

如果一个学生可以在一个招收类,它的是一个这名学生的财产,所以你可以返回下列JSON文档(例如):

If a student can be enrolled in one class, it could be a property of this student, so you could return the following JSON document (for example):

{
  "student": {
    "id": 1,
    "firstName": "John",
    "lastName": "Doe",
    "class": {
      "id": 10,
      "name": "Room Y"
    }
  }
}

您可以得到一个特定的通过以下网址:

You could get a specific class with the following URI:

http://home/someapi/v1/classes/10

您可以得到所有的学生都与此URI给定类:

You could get all students for a given class with this URI:

http://home/someapi/v1/classes/10/students

现在,如果你有一个 NN 学生之间的关系,它会改变我对学生再在JSON presentation第一个建议。其实,关系将成为的资源,而不是嵌入一个

Now, if you have a N-N relation between students and classes, it would change my first proposal about the student representation in JSON. Actually, the class relation would become a collection of class resources, not an embedded property of a student, and you should use the following URI:

http://home/someapi/v1/students/1/classes

含义检索所有类学生#1。

Meaning "retrieve all classes for student #1".

您可以公开既学生资源。无需决定哪一个是主要原因之一,因为他们描述了两种不同的东西。有 /学生 /班允许你创建学生,你可以将它们链接等。

You can expose both class and student resources. No need to decide which one is the main one as they describe two different things. Having /students and /classes allows you to create students and classes, you can link them, etc.

最后一件事,建议使用查询参数(?富=栏)用于过滤的目的。

Last thing, it's recommended to use query parameters (?foo=bar) for filtering purpose.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfggbf
系列文章
更多 icon
同类精品
更多 icon
继续加载