Hypermedia

Hypermedia

In the last few years of my IT experience, I have noticed how every company wants to build microservices, perhaps splitting up an already existing monolith.
Everybody knows what are the advantages of having smaller independent services with a single responsibility, which comunicate between each other through RESTFul APIs.
However, very few companies, no matter if startups or corporations, pay attention to an aspect of microservices which in my opinion is very important: how to represent data to the external world.

With the classic monoliths responsible for showing web pages, we have always cared about how these pages were connected. Otherwise how can our users navigate the website? Yes, I'm talking about links.
Many developers ignore the fact that even RESTFul APIs can (and should) provide links, not between pages but between resources. In which case, instead of calling this relation link, we would call it hypermedia.
Not to mention that they completely ignore the possibility to provide within the response, some useful information like the post data template.
These two things, links and additional information, are strictly related to a well defined and known response format: mediatype.

However the most common situation when we make a GET request to a microservice's endpoint, is to get back a response with nested resources, in some random custom format as mediatype.
As the book RESTful Web APIs says, there are very few situations where a new custom mediatype is needed, in which case it should be well structured, documented, registered to the IANA and possibly made it available to the community.

Mediatypes

Normally it should be enough to reuse media types. Probably the most commons are JSON API (application/vnd.api+json), JSON-LD (application/ld+json), HAL (application/hal+json), SIREN (application/vnd.siren+json), Collection+JSON (application/vnd.collection+json)
Once you adopt one of these types, it will be easy to structure the API response, and start to link resources, rather than nest them. It will be easy to provide also a pagination, a template for the POST and other useful information specific for the resource and generic for the domain.

It is quite clear what are the benefit of reusing mediatypes and adopting hypermedia. Perhaps the only disadvantage is that the frontend team, instead of having all the related resources available, will have to make a new ajax call to the relevant endpoint. However, most of the time, we have to display to the user only some of the data returned by an endpoint, rather than all the nested resources. Therefore, also in this case, using hypermedia will actually be an advantage.

要查看或添加评论,请登录

Antonio Giuliana的更多文章

  • Do teams really implement Continuous Delivery?

    Do teams really implement Continuous Delivery?

    Whenever I changed job I have always encountered the same problem: having a smooth release process, ensuring continuous…

  • Integration test database approaches

    Integration test database approaches

    During my professional experience I have seen different integration test approaches, specifically when it comes to…

社区洞察

其他会员也浏览了