简体   繁体   English

API设计:HATEOAS,json,版本控制和媒体类型

[英]API design: HATEOAS, json, versioning, and media types

I like the REST HATEOAS concept of making your API discoverable through hyperlinks. 我喜欢REST HATEOAS概念,通过超链接使您的API可被发现。 People like XML with ATOM format for links and you might not even need to version your API. 人们喜欢使用带有ATOM格式的XML链接,甚至可能不需要对API进行版本控制。 But, I'm only doing JSON and I want to version my API and still do HATEOAS. 但是,我只是在做JSON而且我想对我的API进行版本控制并仍然使用HATEOAS。

It seems that a best practice would be to use a vendor media type as in application/vnd.me.v1+json but then there is all this talk about no format for hyperlinks in JSON and things like HAL, JSON+Collecton, and Siren which have their own media type. 似乎最好的做法是在application/vnd.me.v1+json使用供应商媒体类型,但是所有这些都是关于JSON中的超链接的格式以及HAL,JSON + Collecton和Siren之类的内容。它们有自己的媒体类型。

So, here's my confusion. 所以,这是我的困惑。 First, why specify a format through media type? 首先,为什么要通过媒体类型指定格式? Why can't the inclusion of links in JSON just be a convention that clients follow? 为什么不能在JSON中包含链接只是客户遵循的约定? Isn't that how hm-json browser discovers links? 是不是hm-json浏览器如何发现链接?

And, if it must be defined as a media type, does something like this work? 并且,如果必须将其定义为媒体类型,那么这样的工作会起作用吗?

application/vnd.me.v1.hal+json

Anyone? 任何人?

First, why specify a format through media type? 首先,为什么要通过媒体类型指定格式?

Yes, you could have a convention for the format of the links, however, any convention could only be discovered after unpacking the body of the HTTP message. 是的,您可以对链接的格式有约定,但是,只有在解压缩HTTP消息的主体后才能发现任何约定。 Since media types are headers, the body of the message can be accepted or rejected as an entire unit. 由于媒体类型是标题,因此可以作为整个单元接受或拒绝消息正文。 This makes processing requests more efficient for consumers that do not support the media types offered. 这使得处理请求对于不支持所提供的媒体类型的消费者更有效。

application/vnd.me.v1.hal+json 应用/ vnd.me.v1.hal + JSON

As far as I understand, media types can be scoped to a level that fits the API you are describing. 据我所知,媒体类型的范围可以适合您所描述的API。 So, you could choose to have a media type for a single representation, a group of representations that all adhere to the same service, or a single media type for the entire organization. 因此,您可以选择具有单个表示的媒体类型,一组表示全部遵循相同服务的表示,或整个组织的单一媒体类型。

This is a good SO post considering media types: 考虑到媒体类型, 这是一篇很好的SO帖子

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

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