简体   繁体   English

RESTful URL设计

[英]RESTful URL design

We have a function to check whether an address is validate. 我们有一个功能来检查地址是否有效。

At first the url is like 起初网址就像

/address/validate/{city}/{state}/{zipCode} /地址/验证/ {城市} / {状态} / {邮政编码}

But later I thought that all the parameters for address is just one resource. 但后来我认为地址的所有参数只是一个资源。 So I changed it to 所以我改成了

/address/validate/{city}.{state}.{zipCode} /address/validate/{city}.{state}.{zipCode}

I'm not sure which one is better, what's your advice? 我不确定哪一个更好,你有什么建议?

In this case I would go for /address/validate?city={city}&state={state}&zipcode={zipCode} 在这种情况下,我会去/address/validate?city={city}&state={state}&zipcode={zipCode}

Why? 为什么? Well it is just my opinion. 嗯,这只是我的意见。 But think of paths (uris) as a unique identifier for a representation. 但是将路径(uris)视为表示的唯一标识符。 Which usually is a part of your model domain. 这通常是您的模型域的一部分。 Since you are not storing (presumably) the result of the validation then just create a validate resource. 由于您没有(可能)存储验证结果,因此只需创建验证资源。

Think of it as a method. 把它想象成一种方法。 And the parameters are query-params. 参数是query-params。

So, doing this, the representation of validate (whether is valid) is modified by the params. 因此,执行此操作时,params会修改validate(无论是否有效)的表示。

Think in rest as a representations, in your example I guess that /address/ exists as a resource, and it has a representation on your data model. 在休息时将其视为表示,在您的示例中,我猜/address/作为资源存在,并且它在您的数据模型上有表示。 So you can POST , PUT and or GET to create, modify and retrieve. 所以你可以POSTPUTGET来创建,修改和检索。

But if you want a /address/validate this is probably the result of the validation. 但是如果你想要一个/address/validate这可能是验证的结果。 SO I'd go with my example. 所以我会选择我的榜样。

First one. 第一。 Becourse it is valid android parseable uri Becourse它是有效的android parseable uri

I think the latter is a better option. 我认为后者是更好的选择。 If a user were later on to validate anyone of the paramaters, and if you add support, it seems more logical to just leave out a param instead of worrying about the order the subpaths are supposed to be in. It ultimately depends on the design of your application as well, as an API design will make more sense for some domains than others. 如果用户以后要验证任何参数,并且如果添加支持,那么放弃一个参数而不是担心子路径应该处于的顺序似乎更合乎逻辑。它最终取决于设计您的应用程序也是如此,因为API设计对某些域比其他域更有意义。 I would add & between the paramaters as well too. 我也会在参数之间添加。

I wouldn't think neither are valid restful URLs. 我不认为它们都不是有效的restful URL。

What your describing can be thought of as "we have a requirement to create an address resource and validate it". 您的描述可以被认为是“我们需要创建地址资源并对其进行验证”。 Why not just POST to /address which will create an address resource with the given city, state and zipCode as the data passed through the POST. 为什么不只是POST到/address将创建一个地址资源与给定的城市,州和zipCode作为通过POST传递的数据。 Now in that logic you would validate if the resource can be created. 现在,在该逻辑中,您将验证是否可以创建资源。 If it can then you would create it and return a unique reference for that resource, which will be used in subsequent requests /address/:id/... . 如果可以,那么您将创建它并返回该资源的唯一引用,该引用将在后续请求/address/:id/... Otherwise if a validation error occurs you would return a HTTP error code 400 Bad Request . 否则,如果发生验证错误,您将返回HTTP错误代码400 Bad Request

This is probably more 'Restful' in my opinion. 在我看来,这可能更“宁静”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM