REST:将 URL 映射到服务/文件的方法?

发布于 2024-08-21 01:05:57 字数 61 浏览 4 评论 0原文

以您的愚见:将 REST URL 映射到架构内的服务/文件的最佳实践方法是什么(让我们假设 MVC 模式)?

In your humble opinion: what would be a best practice aproach to map REST URLs to services/files within one's architecture (let's assume MVC pattern here)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2024-08-28 01:05:58

将 URL 映射到资源的最佳方法取决于您用来提供 REST 服务的 Web 框架。选择最容易使用您拥有的工具进行管理的 URL 结构。

url 结构对于服务的客户端应该是完全不透明的,因此他们不应该关心它们的外观。

我认为最重要的是,当您查看 URL 时,应该相对容易猜测服务器上的哪个控制器将响应该 URL。这将使开发和调试变得更加容易。

The best way to map URLs to resources is dependent on what web framework you use to provide the REST service. Pick whatever url structure is easiest to manage with the tools you have.

The url structure should be completely opaque to the clients of your service so they should not care what they look like.

The most important thing in my opinion is that when you are looking at an URL it should be relatively easy to guess which controller on the server is going to respond to that URL. That will make development and debugging much easier.

南薇 2024-08-28 01:05:57

除了 Darrel 的回答之外:

利用 HTTP URI 的分层性质;将每个路径段视为深入到托管项目(例如订单、客户)的整体空间。如果在某些时候您需要沿多个维度(例如查询)对集合进行“索引”,则使用查询字符串参数:

/service/products/cars/japanese-cars/toyota/corola/&priceMin=2000&priceMax=5000

请注意(正如达雷尔所说)该结构对客户端应该是不透明的。这意味着客户端需要在运行时发现参数(这就是表单或 URI 模板的用途)。当然,客户端和服务器需要共享有关priceMin 的含义的知识。该知识应该存在于某些设计时规范中,例如链接关系的规范。也许查看 http://www.opensearch.org 了解详细的用例。

同样有趣的是 URI 的主机部分。如果您可能在某个阶段需要将部分服务移动到另一台计算机,请设计您的 URI,以便相关信息位于域部分中。然后您可以使用简单的 DNS 将请求路由到不同的计算机。

哈特哈,

In addition to Darrel's answer:

Make use of the hierarchical nature of HTTP URIs; think of every path segment as drilling down into the overall space of managed items (e.g. orders, customers). If at some point you need to 'index' into a collection along multiple dimensions (e.g. query) then use query string parameters:

/service/products/cars/japanese-cars/toyota/corola/&priceMin=2000&priceMax=5000

Note that (as darrel said) the structure should be opaque to the client. That means that the client needs to discover the parameters at run time (that is what forms or URI templates are for). Of course client and server need shared knowledge about the meaning of e.g. priceMin. That knowledge should be in some design time specification, for example the specification of a link relation. Maybe look at http://www.opensearch.org for detailed use case.

Also interesting is the host part of the URIs. If you might at some stage need to move parts of your services to another machine, design your URIs so that the relevant information is in the domain part. Then you can use simple DNS to route requests to different machines.

HTH,
Jan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文