Introduction to Hateoas in Spring Rest
Go Back to Course Outline Repositories https://github.com/terawarehouse/terawarehouse-catalog https://github.com/terawarehouse/tera...
https://www.czetsuyatech.com/2019/07/spring-rest-hateoas-introduction.html
Repositories
- https://github.com/terawarehouse/terawarehouse-catalog
- https://github.com/terawarehouse/terawarehouse-react
HATEOAS or Hypertext as the Engine of Application State is a guideline for the client to navigate to the relevant information for a given resource. For example, if you have an entity resource, it makes sense to also return the list URL for that entity as well as the link for the entity itself.
[open CategoryController class]
There are 2 main classes that we need to remember when implementing HATEOAS, Resource, and Resources. Resource must be parameterized with the entity that it is handling. For example Resource
In the one() method, the category object is wrapped into a Resource object and links are set before it is returned.
[show one() method]
As the name implies, Resources takes on an array of Resource<> eg. Resources
[show all() method]
Now start the Spring boot application.
[run oneCode and all tests in Postman]
[open categoryResourceAssembler class]
If you will notice in our class we have this categoryResourceAssembler class. It contains a utility function that wraps an entity in a Resource object. It’s really important as it saves you a lot of time writing the common links for a given entity.
There are times when we simply want a subset of the entity’s field in the response and that’s what we will present in the next video. So see you there!
Post a Comment