REST最佳实践命名嵌套资源 [英] REST best practice for naming nested resources

查看:158
本文介绍了REST最佳实践命名嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有以下资源:

/api/user/12

返回有关与ID的用户信息= 12

returns information about user with id=12

/api/user/12/trips

返回由用户12多次前往列表以及他们的信息

returns a list of trips made by user 12 and their information

不过,我认为/旅行/单独的资源,所以这将继续是这样的:

However I consider /trip/ a separate resource, so this would continue like this:

/api/trip/5

返回关于与ID行程信息= 5

returns information about trip with id=5

这是一个很好的方式做到这一点?

Is this a good way to do this?

推荐答案

您应该总是用复数来命名的资源:

You should always use plural to name resources:

/api/users

含义:所有用户(集合)

Meaning: all users (collection).

/api/users/12

含义:用户12

Meaning: user 12.

有关涉及给定资源嵌套的资源,你可以这样做:

For nested resources related to a given resource, you can do:

/api/users/12/trips

含义:所有的车次用户 12

不过,你仍然可以描述一个特定的之旅使用这个网址:

But, you still can describe a specific trip using this URI:

/api/trips/5

含义:行程5

这篇关于REST最佳实践命名嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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