如何营造一个宁静的方式基于一些表决定的资源 [英] How to decide a resource in a restful way based on some tables
问题描述
我需要知道如何决定什么是资源,什么是子资源。我只是有一些表的,我需要做的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?
推荐答案
我建议处理两个资源:学生
和类
。
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
请注意复数这里,如果你想了解为什么你应该用复数看看这篇文章:的http://blog.apigee.com/detail/restful_api_design_plural_nouns_and_concrete_names/.
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第一个建议。其实,类
关系将成为类
的资源,而不是嵌入一个学生
,你应该使用以下URI:
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.
这篇关于如何营造一个宁静的方式基于一些表决定的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!