2011-01-31 71 views
2

我要找来查询一个REST方式下面用例一种独特的方式进行建模: -如何存储库,提供智能的基于REST访问

假设库包含以下内容: -

a. green color ball image of 1cm radius 
b. yellow color ball image of 1cm radius 
c. blue color ball image of 1cm radius 
d. green color ball image of 2cm radius 
e. yellow color ball image of 2cm radius 
f. blue color ball image of 2cm radius 
g. computer monitor icon image of size 32x32 pixels in png format 
h. computer monitor icon image of size 64x64 pixels in png format 
i. computer monitor icon image of size 32x32 pixels in ico format 
j. computer monitor icon image of size 64x64 pixels in ico format 
k. HR travel policy 
l. HR new hire policy 
g. HR promotion policy 

1. Find all documents published after a certain date? 
2. Find all documents published before a certain date? 
3. Find all documents published between a certain set of dates? 
4. Find all balls which are 1cm in radius 
5. Find all documents whose download format is "png" 
6. Find all documents whose size is 32x32 pixels 
7. Find all balls which are green in color. 

我们的存储库可能基于Google存储,Amazon S3,Mongodb GridFS,Java内容存储库(JCR 2.0)或简单的文件系统。

什么是理想的方式来存储和检索上述数据。我希望REST URL尽可能具有表达性,以便我可以对上述任何用例进行建模[1-6]。欣赏任何有关如何设计通用知识库的指针,以便我可以使用适当的命名约定来基于上述查询来获取文档。

+1

Java内容存储库可能是最适合您的需求 - 也考虑阿帕奇吊带内置了JCR顶上一个REST框架:http://jackrabbit.apache.org/。此外,我怀疑,但不知道,其他NoSQL解决方案在将二进制数据存储为JCR时并不那么灵活。 – orangepips 2011-01-31 19:33:21

回答

2

我建议在此分离两个问题:1)建模存储库; 2)设计RESTful访问。当一个开发刚刚开始时,这两个问题总是齐头并进。随着项目的发展,您可能需要设计汇总点或仅为存储库提供不同的视点。理解这些概念只在表面上相似总是很好的。

至于你的7个问题去的,首先你需要决定是否“文件”的概念是由“文档实例”的概念完全更换。例如,如果红球的ID为1,那么以下两个资源是否有效?

/documents/1 
/balls/1 

你可以发布一个新的球到以下两个资源或只是一个?

/documents/ 
/balls/ 

您是否可以通过单个或多个资源查找所有绿球?

/documents/type/ball/by/color/green 
/balls/by/color/green 

回答这些问题非常重要,以便您的API毫不含糊。

其次,你需要决定如何“搜索引擎友好的”你希望你的资源的样子。例如:

1. /documents/after-date/2001-01-01/ 
2. /documents/before-date/2010-12-31/ 
3. /documents/after-date/2001-01-01/before-date/2010-12-31/ 
7. /balls/by/color/green 

or 

1. /documents?after-date=2001-01-01 
2. /documents?before-date=2010-12-31 
3. /documents?after-date=2001-01-01&before-date=2010-12-31 
7. /balls?color=green 

这是一个“关注”,如果你的API是敞开的,并用来驱动一个公共网站链接。我之所以把“搜索引擎优化”和“关注”放在引号中是因为搜索引擎优化专家仍然不同意搜索引擎是否喜欢这种或那种引擎,或者引擎根本不关心。

从实现的角度来看,使用URL参数更容易,更快,更具可扩展性。但是,如果你不期望疯狂的组合数量,那么这两种方法都可以很好地工作。

从个人的经验,我也建议以组搜索类型的资源(如反对身份资源)有共同的路径。例如:

7. /balls/search?color=green 

instead of 

7. /balls?color=green 

祝你好运与设计。谈到REST时没有一个正确的方法。只要它是有道理的 - 你走在一条好路上。

相关问题