简体   繁体   English

如何在Angular中使用HATEOAS REST API?

[英]How to consume a HATEOAS REST API in Angular?

I'm working on an Angular 4 front-end for an API built by another team. 我正在开发一个Angular 4前端,用于由另一个团队构建的API。 The API follows HATEOAS and provides me with hypermedia links with every single response. API遵循HATEOAS并为我提供每个响应的超媒体链接。

I know the shape of the API and I figure I can just hard-code the URLs into Angular Services with minimal fuss. 我知道API的形状,我想我可以简单地将URL硬编码到Angular Services中。 However, a colleague (who is a backend developer) is trying to convince me that I should take full advantage of the hypermedia because it will mean less coupling between the frontend and backend (and potential breakage if the API changes). 然而,一位同事(他是一名后端开发人员)试图说服我,我应该充分利用超媒体,因为它意味着前端和后端之间的耦合较少(如果API发生变化,可能会导致破坏)。

However, I'm stumped on how I'd even go about implementing a simple HATEOAS pattern using Angular's built-in Http service. 但是,我对如何使用Angular的内置Http服务实现简单的HATEOAS模式感到困惑。 How would I store/share the hypermedia/URL information in a way that doesn't couple all the services together and make them hard-to-test? 如何以不将所有服务连接在一起并使其难以测试的方式存储/共享超媒体/ URL信息? There seems to be no examples out there. 似乎没有任何例子。

Would trying to create a HATEOAS-friendly HTTP client even be a good idea, or is it likely not worth the trouble? 尝试创建一个HATEOAS友好的HTTP客户端甚至是一个好主意,还是可能不值得麻烦?

Your colleague is right, you should use the meta information that the back-end provides. 你的同事是对的,你应该使用后端提供的元信息。 In this way you are not putting responsibility on the client that doesn't belong there. 通过这种方式,您不会对不属于那里的客户负责。 Why should the client know from where to fetch the entities? 客户为什么要知道从哪里获取实体? Storing the entities (in fact the data in general) is the responsibility of the back-end. 存储实体(实际上是一般的数据)是后端的责任。 The back-end owns the data, it decides where to put it, how to access it, when to change the location or the persistence type, anything related to storing the data. 后端拥有数据,它决定在何处放置数据,如何访问数据,何时更改位置或持久性类型,以及与存储数据相关的任何内容。

How would I store/share the hypermedia/URL information in a way that doesn't couple all the services together and make them hard-to-test? 如何以不将所有服务连接在一起并使其难以测试的方式存储/共享超媒体/ URL信息?

Why do you think using HATEOAS makes the testing harder? 为什么你认为使用HATEOAS会使测试变得更难? It does not, in fact not using it makes the testing harder as the URLs are static which makes the back-end non-stub-able. 它实际上没有使用它会使测试变得更难,因为URL是静态的,这使得后端不可存根。

You can extract the information from the back-end response and store it as meta-information in the angular model, on a _meta key or something like that. 您可以从后端响应中提取信息,并将其作为元信息存储在角度模型中, _meta键或类似的东西上。

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

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