对其他资源的副作用 [英] Side effects on other resources

查看:57
本文介绍了对其他资源的副作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于 RESTful 和 HATEOAS 设计的理论/最佳实践问题...

This is a theory / best practices question regarding RESTful and HATEOAS design...

鉴于资源:

/myresources/(我们的资源对象的集合)

/myresources/ (a collection of our resource objects)

/transactions/(系统中发生过的历史交易的集合)

/transactions/ (a collection of historical transactions that have occurred in the system)

对于以下情况是否有效:

Is it a valid practice for:

POST/myresources/

POST /myresources/

不仅要在/myresources/创建新资源,还要在/transactions/创建新资源?

to not only create a new resource at /myresources/ but also a new resource at /transactions/?

换句话说,对一个 URL 的 POST(或任何动词)可以影响该 URL 和其他 URL 上的资源吗?还有另一种方法吗?显然我们可以使用两个 POST,但这需要我们信任用户在多资源修改中保持有效状态.

In other words, can a POST (or any verb) to one URL effect resources at both that URL and others? Is there another approach? Obviously we could use two POSTs, but that requires us to trust the user to maintain valid state across multi-resource modifications.

推荐答案

是的,这很好.想象另一个系统公开 /myresources/latest URI 的情况.当没有资源时,可能会返回 404,但是当您开始发布资源时,规范 URI 和 latest URI 都将返回 200 OK.这种方法有很多很多有用的好处.

Yes, this is fine. Imagine another case where the system exposes a /myresources/latest URI. When there are no resources, that might return 404, but when you start POSTing resources, both the canonical URI and the latest URI will return 200 OK. There are many, many useful benefits to this approach.

但是,在设计此类资源时请牢记缓存.例如,如果您 POST 到 /myresources/ 集合,您将在沿途的任何缓存中使该集合无效.但是,您不会使 /transactions/ 集合无效,并且两个索引可能会不同步.无论如何,它们可能在整个系统中不同步,具体取决于多个客户端和源服务器之间的缓存图,但通常,客户端被设计为期望远距离的此操作是同步的,并且缓存可以在这种情况下,请不要沮丧.

However, keep caching in mind while you design such resources. If you POST to the /myresources/ collection, for example, you will invalidate that collection in any caches along the way. You will not, however, invalidate the /transactions/ collection, and the two indices could get out of sync. They can be out of sync across the whole system anyway, depending on the graph of caches in between multiple clients and the origin server(s), but often, clients get designed to expect this action at a distance to be synchronous, and caching can frustrate that in cases like this.

这篇关于对其他资源的副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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