RESTful枚举。字符串或ID? [英] RESTful enums. string or Id?

查看:285
本文介绍了RESTful枚举。字符串或ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个暴露枚举的RESTful服务。



我应该将其公开为本地化字符串还是纯整数?



我的倾斜朝向整数,以便在服务端轻松转换,但在这种情况下,客户端需要从某个地方获取本地化字符串列表,以便知道枚举的含义。我只是创建额外的步骤没有什么?



似乎有一些信息,我可以找到通常在RESTful API中完成。



编辑:



确定。假设我正在编写一个网站来存储有关人们宠物的信息。我可以有一个AnimalType枚举

  0狗
1猫
2兔子

当人们抓住特定的宠物资源时,说 / pets / 1 ,我可以为动物类型提供有意义的本地化字符串,也可以提供ID,并强制他们通过 / pets / types 资源。



还是应该提供两个?

解决方案

猜测你应该考虑一下您要向客户公开的RESTful API。



您自己在代码中使用枚举是一个实现细节。
您的客户端将在执行过程中使用枚举,既不知道也不应该打扰您,因为这只是不同的抽象级别。



API设计第一,实现如下。



如果您提供更多的洞察信息,您希望使用这些枚举返回给您的客户端,我可以更直接的回答。



现在我猜,如果整数ids和字符串描述与描述信息或状态有关,您希望在调用RESTful资源时返回,那么您将返回它们都包含在一个json或xml文档。


I have a RESTful service that exposes enums.

Should I expose them as localised strings, or plain integers?

My leaning is toward integers for easy conversion at the service end, but in that case the client needs to grab a list of localised strings from somewhere in order to know what the enums mean. Am I just creating extra steps for nothing?

There seems to be little information I can find about which is commonly done in RESTful APIs.

EDIT:

OK. Let's say I'm writing a website that stores information about people's pets. I could have an AnimalType enum

0 Dog
1 Cat
2 Rabbit 
etc.

When people grab a particular pet resource, say /pets/1, I can either provide a meaningful localised string for the animal type, or just provide the ID and force them to do another look up via a /pets/types resource.

Or should I provide both?

解决方案

I guess you should think in therms of consistent RESTful API you want to expose to your clients.

Wheter you yourself use enums in your code or not is an implementation detail. Wheter your client will use enums in his implementation you neither know nor should that bother you because that's just different level of abstraction.

API design first, implementation follows.

If you provide a bit more insight of what kind of information you want to return to your client using these enums I could give more straight answer.

For now I guess if integer ids and string descriptions are relevant in describing information or state you want to return in call to your RESTful resource then you shuould return them both wrapped in a json or xml document.

这篇关于RESTful枚举。字符串或ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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