用户的 REST 路由资源

REST route resource by user

我有很多资源,如产品、预订等。需要路由来按用户获取所有资源。

例如:

{GET} /bookings/user

我认为这是不正确的,因为这条路线会 return 用户资源。还是我错了?

正确的方法是什么?

通常,人们会为此使用查询参数:

GET /bookings?userId={userId}

这将 return 给定用户 ID 的所有预订。

I think this is incorrect, because this route would return user resource. Or I am wrong?

它returns 表示由/bookings/user 标识的资源。那是什么资源?由源服务器决定。

不关心您使用什么拼写约定作为您的标识符。就 REST 而言,/6414b60b-1ecb-4e28-8887-4bfe120810e7 是一个非常令人满意的资源标识符。

如果您想要人类可读的标识符,那也没关系。 RFC 3986 鼓励这种做法:

A URI often has to be remembered by people, and it is easier for people to remember a URI when it consists of meaningful or familiar components.

您还应该记住,RFC 3986 在 URI 中区分分层和非分层信号

The URI syntax is organized hierarchically, with components listed in order of decreasing significance from left to right. -- section 1.2.3

The path component contains data, usually organized in hierarchical form, that, along with data in the non-hierarchical query component -- section 3.3

The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI's scheme and naming authority (if any) -- section 3.4

简而言之,如果您描述的信息是在层次结构中描述的,那么使用路径段就很有意义

I don`t return users and user ids, need return all booking for authorized user.

这听起来像是在描述 /my/bookings.

这样的拼写

可能有助于 URI 设计的启发式方法是考虑 relative references 的好处;与文件系统一样,您可以使用点段导航至命名空间的根。

`/my/bookings` + `../profile` --> `/my/profile`

个您可能不想支持的原因 hackable URI;所以您需要仔细考虑该约束及其含义。