如何为RESTful Web服务创建自定义媒体类型(应用程序/vnd)? [英] How to create a custom media type (application/vnd) for a RESTful web service?

查看:132
本文介绍了如何为RESTful Web服务创建自定义媒体类型(应用程序/vnd)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用REST,并认为我正确实现了 HATEOAS 只是为了使所有概念正确.

I'm playing with REST right now and thought I properly implement HATEOAS just to get all concepts right.

为此,我想创建自己的媒体类型(application/vnd[...]+xmlapplication/vnd[...]+json).

For that I want to create my own media types (application/vnd[...]+xml and application/vnd[...]+json).

第一个问题:媒体类型是否定义了服务器与客户端之间的合同?

媒体类型将定义我的消息格式,因此我需要添加XML模式和JSON模式以与新的媒体类型一起使用(以便REST客户端知道消息中包含什么以及发回什么内容).

The media type will define my message formats so I need to add XML schema and JSON schema to go with the new media types (so that REST clients know what's coming in messages and what to send back).

我已经在网络上进行了一些研究,但有关如何执行此操作的详细信息丢失了.它仅涉及编写详尽的规范/文档还是有一些技术步骤要实现? (我不必在IANA上注册吗?)

I've done some research on the web but the details of how one does this are missing. Does it only involve writing exhaustive specification/documentations or are there some technical steps to implement? (I don't have to register it with IANA do I?)

如何创建新的-功能齐全的application/vnd媒体类型?还有什么需要照顾的,以便客户可以正确使用它?

How can a new - fully functional - application/vnd media type be created? and what do you need to take care of so that clients can properly use it?

推荐答案

@JohnDoDo

@JohnDoDo

第一个问题:媒体类型是否定义了我的服务器和客户端之间的合同?

One first question: Does the media type define the contract between my server and client?

是的,媒体类型是合同的一部分.与SOAP(即WSDL)不同,REST API中的合同不是静态的.合同是通过基础协议(即HTTP),URI和媒体类型(不禁止同时使用多种媒体类型)的组合来定义的.媒体类型定义了数据模型,处理模型,超媒体控件(即带注释的链接,输入表单等),并支持包含由链接关系,元素名称,ID,类名称等描述的其他应用程序特定信息.

Yes, media type is one part of the contract. Contract in REST API is not static unlike SOAP(i.e. WSDL). Contract is defined by combination of underlying protocol(i.e. HTTP), URIs and Media Types(it's not prohibited to use several media types together). Media type defines data model, processing model, hypermedia controls(i.e. annotated links, input forms etc...) and support to include additional application specific information described by link relations, element names, ids, class names etc...

媒体类型将定义我的消息格式,因此我需要添加XML模式和JSON模式以与新的媒体类型一起使用(以便REST客户端知道消息中包含什么以及发回什么内容).

The media type will define my message formats so I need to add XML schema and JSON schema to go with the new media types (so that REST clients know what's coming in messages and what to send back).

您只需要定义覆盖文档结构的通用模式即可.您无需为特定消息定义单独的架构.您的消息必须位于由媒体类型定义的结构中.

You only need to define generic schemas which cover structure of the document. You do not need to define separate schemas for particular messages. Your messages must feet in the structure defined by media type.

如何创建新的功能齐全的应用程序/vnd媒体类型?您需要注意什么,以便客户可以正确使用它?

How can a new - fully functional - application/vnd media type be created? and what do you need to take care of so that clients can properly use it?

  1. 描述它(即写格式规范);
  2. 向IANA注册: http://www.iana.org/cgi-bin/mediatypes.pl 在vnd.*树下注册媒体类型大约需要一个星期的时间.
  1. Describe it(i.e. write format specification);
  2. Register with IANA: http://www.iana.org/cgi-bin/mediatypes.pl registering media type under vnd.* tree takes nearly one week to register.

这篇关于如何为RESTful Web服务创建自定义媒体类型(应用程序/vnd)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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