这是关于服务架构策略的更多问题,我们正在基于后端的休息服务构建大型Web系统。目前我们正在尝试制定一些内部标准,以开发其他服务。RESTful服务架构问题
某些查询返回的实体的列表,例如让我们考虑我们的图片库中检索服务:/ gell_all_galeries,返回下一个响应:
<galleries>
<gallery>
<id>some_gallery_id</id>
<name>my photos</name>
<photos>
<photo>
<id>123</id>
<name>my photo</name>
<location>http://mysite/photo/show/123</location>
......
<author>
<id>some_id</id>
<name>some name</name>
.......
<author>
</photo>
<photo> ..... </photo>
<photo> ..... </photo>
<photo> ..... </photo>
<photo> ..... </photo>
</photos>
</gallery>
<gallery> .... </gallery>
<gallery> .... </gallery>
<gallery> .... </gallery>
<gallery> .... </gallery>
</galleries>
正如你在这里看到,响应相当大又重,而且并不总是我们需要这样的深层信息。通常的解决方法是使用或每家画廊http://ru.wikipedia.org/wiki/Atom元素全库数据,而不是:
<galleries>
<gallery>
<id>some_gallery_id</id>
<link href="http://mysite/gallery/some_gallery_id"/>
</gallery>
<gallery>
<id>second_gallery_id</id>
<link href="http://mysite/gallery/second_gallery_id"/>
</gallery>
<gallery> .... </gallery>
<gallery> .... </gallery>
<gallery> .... </gallery>
<gallery> .... </gallery>
</galleries>
的第一个问题,是未来:也许相反,我们甚至不应该使用和类型,以及只使用通用的所有资源该返回列表对象:
<list>
<item><link href="http://mysite/gallery/some_gallery_id"/></item>
<item><link href="http://mysite/gallery/other_gallery_id"/></item>
<item>....</item>
</list>
而第二个问题,用户以后尝试检索有关一些具体画廊的信息,他将使用例如http://mysite/gallery/some_gallery_id链接,他应该怎么看到的结果吗?
它应该是:
<gallery>
<id>some_gallery_id</id>
<name>my photos</name>
<photos>
<photo>
<id>123</id>
<name>my photo</name>
<location>http://mysite/photo/show/123</location>
......
<author>
<id>some_id</id>
<name>some name</name>
.......
<author>
</photo>
<photo> ..... </photo>
<photo> ..... </photo>
<photo> ..... </photo>
<photo> ..... </photo>
</photos>
</gallery>
或:
<gallery>
<id>some_gallery_id</id>
<name>my photos</name>
<photos>
<photo><link href="http://mysite/photo/11111"/></photo>
<photo><link href="http://mysite/photo/22222"/></photo>
<photo><link href="http://mysite/photo/33333"/> </photo>
<photo> ..... </photo>
</photos>
</gallery>
或
<gallery>
<id>some_gallery_id</id>
<name>my photos</name>
<photos>
<photo>
<link href="http://mysite/photo/11111"/>
<author>
<link href="http://mysite/author/11111"/>
</author>
</photo>
<photo>
<link href="http://mysite/photo/22222"/>
<author>
<link href="http://mysite/author/11111"/>
</author>
</photo>
<photo>
<link href="http://mysite/photo/33333"/>
<author>
<link href="http://mysite/author/11111"/>
</author>
</photo>
<photo> ..... </photo>
</photos>
</gallery>
我的意思是,如果我们使用的链接,而不是完整的对象的信息,有多深,我们应该去那里?我是否应该在照片中显示作者等等。
大概是我的问题含糊不清,但我想要做的是在这种情况下为所有团队成员在未来遵循一般策略。
我使用Java和Apache CXF来提供RESTful服务。这具有能够为相同资源提供* XML和JSON两者的优点,这取决于客户说它喜欢什么(即通过内容协商)。 – 2010-06-08 12:46:50