Explore upgrading to hypermedia-driven APIs.
- [Instructor] A cool feature of Spring Data REST is that it provides hypermedia embedded URLs in the HTTP response bodies. Clients can drill down into data elements just by invoking HTTP Get on the URLs. This is known as hypermedia as the engine of application state, or HATEOAS. Yes, it's a terrible acronym. Unlike Spring Data REST, web MVC REST controllers require extra coding to implement HATEOAS.
The Spring HATEOAS dependency is included by Spring Data REST, so we don't need to add the dependency to our POM file. I have a new class, which is a ratings controller, in the web folder and it is mapped with slash ratings, and we have a Get All method that looks up all the tour ratings, and returns it on the API, and a Get Ratings by ID, and it returns one particular tour rating.
So let's run this and invoke the slash ratings API. We see here that there is no link back to the tour rating itself, and worse yet, the API returns all the attributes of the rated tour. This is a bloated response body with duplication where many ratings are associated with the same tour. So let's see how we can add HATEOAS enablement to this API. First, we cannot return the entity as it is.
We need to copy them into a data transfer object. Happily, we already have a ratings DTO. But we need one small update here. We must extend it from org.springframework.hateoas.resourcesupport. Then, we need to create a class that extends from Resource Assembler Support. In web package I've already created the class called Rating Assembler.
So Rating Assembler extends from Resource Assembler Support and the first generic parameter is the tour rating and the second parameter is the rating DTO. We must implement the two resource method, which converts the tour rating entities into rating DTOs. First, we construct a rating DTO. Then because rating DTO extends from Resource Support, several methods are available for adding hypermedia enabled links.
We can, if we wanted to, hard code the URL path for each of the links. However, the Spring HATEOAS controller link builder and repository entity links are able to derive the paths for us. With controller link builder, we get the URL from the ratingcontroller.getratingmethod and pass the tour rating ID in. Then, add the link to the DTO where the key is Self. Entity links is injected into this component and provides a look up into Spring Data REST repositories.
So we look up the tour repository and pass in the tour ID. Then, add that link to the DTO where the key is Tour. And finally, we return the DTO. Now let's use this rating assembler in our ratings controller class. First we need to inject the ratings assembler.
And then, invoke the two resource method in Get Rating and return the rating DTO instead of the tour rating entity. Next, invoke the two resources method on Get All.
So, both the two resource and the two resources method will take either one rating entity or a list of tour rating entities and convert them to either a rating DTO or a list of rating DTOs with the links embedded inside them. So let's stop our application and rerun it. And let's invoke the ratings API.
And we see, here's all our list of ratings and we have embedded links inside, so let's drill down into one rating. Invoke a Get on that. That would be just a Get One. And so we have these self hyperlink, goes back to itself, as well as the hyperlink to the tour. So we can even drill down into the tour with this. Voila! Embedded hypermedia.
- Enhancing a Spring Boot microservice
- Hardening the microservice
- Configuring security with JSON web tokens
- Leveraging Docker for MySQL database access
- Dockerizing your microservice