简体   繁体   English

是否应该在 RESTful API 中使用视图

[英]Should Views be used in RESTful APIs

I guess this is relative to API's best practices.我想这与 API 的最佳实践有关。 I am looking at implementing API and not really sure if I need to use views in API design or leave them for user's frontend implementation.我正在考虑实现 API,但不确定是否需要在 API 设计中使用视图或将它们留给用户的前端实现。 I don't think is a good idea to implement views in API, but it would be nice to know what usually is done.我认为在 API 中实现视图不是一个好主意,但是知道通常会做什么会很好。 I think API calls should always return either JSON or XML;我认为 API 调用应该总是返回 JSON 或 XML; adding views complicates everything...添加视图使一切变得复杂......

Do you use views in APIs, yes/no, why?您是否在 API 中使用视图,是/否,为什么?

API stands for Application Programming Interface. API 代表应用程序编程接口。 It is something exposed by your app that can be called to interact with it.它是您的应用程序公开的内容,可以调用它与它进行交互。 REST is a philosophy which does not tell you how to design your application, just defines a communication structure (resources, representations, verbs, nouns). REST 是一种哲学,它不会告诉您如何设计应用程序,而只是定义了一种通信结构(资源、表示、动词、名词)。

A view is not a concept in REST, but in the most common case, a view will be a representation of a resource in REST.视图不是 REST 中的概念,但在最常见的情况下,视图将是 REST 中资源的表示。 Although this view representation can be directly returned by the server, most developer prefer to return only the data and separate the visualization.虽然这种视图表示可以由服务器直接返回,但大多数开发人员更喜欢只返回数据并分离可视化。

If your API returns HTML, that would not violate REST, but it would introduce coupling between the user interface and data of your application.如果您的 API 返回 HTML,那不会违反 REST,但会在用户界面和应用程序数据之间引入耦合。 I recommend that you don't do it, not because of REST, but because you'd violate basic software design principles.我建议您不要这样做,不是因为 REST,而是因为您违反了基本的软件设计原则。

For my REST APIs only JSON data.对于我的 REST API,只有 JSON 数据。 In REST APIs you must return JSON or XML because your API needs to be indenpendant of the frontend implementation.在 REST API 中,您必须返回 JSON 或 XML,因为您的 API 需要独立于前端实现。

The view you are suggesting is only the html kind of view.您建议的视图只是 html 类型的视图。 But a view can also be in json, xml and so many other types.但是视图也可以是 json、xml 和许多其他类型。

View is a representation of the resource.视图是资源的表示。 Is the way you export the data processed by your application.是您导出应用程序处理的数据的方式。

Thinking like that, u will notice that even and api project can use MVC.这么想,你会注意到 even 和 api 项目可以使用 MVC。

There are a couple of gems to work with views on an api project like: rabl and active_model_serializers .有几个 gem 可以处理 api 项目的视图,例如: rablactive_model_serializers

The answer is yes and no.答案是肯定的和否定的。 A REST API that outputs simple machine-parseable format (XML, JSON, CSV) doesn't need views, but it helps API users, if they can see the output in a human readable format, so they can use it to verify the output of their application consuming the REST API.输出简单的机器可解析格式(XML、JSON、CSV)的 REST API 不需要视图,但它可以帮助 API 用户,如果他们可以看到人类可读格式的输出,那么他们就可以使用它来验证输出他们的应用程序使用 REST API。 It also a valid option, if you minimize code duplication by handling data processing in the model, and you have separate views for a JSON/XML REST API and another for human consumption.如果您通过处理模型中的数据处理来最大程度地减少代码重复,并且您有单独的 JSON/XML REST API 视图和另一个供人类使用的视图,那么这也是一个有效的选择。 In this case, the "view" transforms the objects created/retrieved by the model to various output formats to various consumers -- both human and programmatic.在这种情况下,“视图”将模型创建/检索的对象转换为各种输出格式,以提供给各种使用者——人类和编程。

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

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