按不同字段获取资源的 RESTful url [英] RESTful url to GET resource by different fields

查看:34
本文介绍了按不同字段获取资源的 RESTful url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,我很难找到答案..

Simple question I'm having trouble finding an answer to..

如果我有一个 REST 网络服务,并且我的设计没有使用 url 参数,我如何指定两个不同的键来返回相同的资源?

If I have a REST web service, and my design is not using url parameters, how can I specify two different keys to return the same resource by?

示例我想要(并且已经实施)

Example I want (and have already implemented)

/Person/{ID}

按预期返回一个人.

现在我也想要

/Person/{Name}

按姓名返回一个人.

这是正确的 RESTful 格式吗?或者是这样的:

Is this the correct RESTful format? Or is it something like:

/Person/Name/{Name}

推荐答案

您应该只使用一个 URI 来引用单个资源.拥有多个 URI 只会导致混淆.在您的示例中,由于两个人的名字相同,会引起混淆.那么他们指的是哪个人员资源?

You should only use one URI to refer to a single resource. Having multiple URIs will only cause confusion. In your example, confusion would arise due to two people having the same name. Which person resource are they referring to then?

也就是说,您可以有多个 URI 引用单个资源,但对于真实"URI 以外的任何内容,您应该使用状态代码 301 - Moved Permanently<简单地将客户端重定向到正确的位置.

That said, you can have multiple URIs refer to a single resource, but for anything other than the "true" URI you should simply redirect the client to the right place using a status code of 301 - Moved Permanently.

就我个人而言,我永远不会实施多 ID 方案或重定向来支持它.选择一个单一的识别方案并坚持下去.你的 API 的用户会感谢你.

Personally, I would never implement a multi-ID scheme or redirection to support it. Pick a single identification scheme and stick with it. The users of your API will thank you.

您真正需要构建的是查询 API,因此请关注如何实现诸如 /personFinder 资源之类的东西,该资源可以将名称作为参数并返回可能有多个匹配的 /person/{ID} 响应中的 URI.

What you really need to build is a query API, so focus on how you would implement something like a /personFinder resource which could take a name as a parameter and return potentially multiple matching /person/{ID} URIs in the response.

这篇关于按不同字段获取资源的 RESTful url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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