Lesson Description

The "Spring HATEAOS" Lesson is part of the full, Enterprise Java with Spring Boot course featured in this preview video. Here's what you'd learn in this lesson:

Josh introduces HATEAOS, hypermedia as the engine of application state. This pattern provides link metadata along with the payload so the rest client does not require prior knowledge of interacting with an application or server beyond a generic understanding of hypermedia.

Preview
Close

Transcript from the "Spring HATEAOS" Lesson

[00:00:00]
>> Josh Long: So we have a project called Spring Hateous. Okay, this is Spring and then this is an acronym. It means Hypermedia as the Engine of Application State. This is a pattern that predates Spring's support of it, right? We created a framework to support the pattern, but the pattern existed long before we did.

[00:00:23]
And so, it's basically a separate project. It builds on top of what we've just looked at and I happen to have it on the class path. So what I'm going to do now is I'm going to build a very quick sort of hypermedia friendly version of this code.

[00:00:36]
So let's say, and by the way, in the meantime, since this is not rest, I'll restore this to response body and addcontroller. Once we've moved to hypermedia, I'll feel ok. Adding back response body, sorry, Rest Controller. Okay, so let's do this. So we've got this, this is the old one.

[00:00:59]
Okay, this is an HTTP endpoint, but let's do hypermedia endpoint, right? So let's just do users collection, and I'm gonna return entity model of users, okay? And in order to get an entity model of users, I need to have a representation model assembler. Okay, Representation model assembler for user.

[00:01:28]
Entity model user. So let me see. Entity model. Well, let me build that first and we'll get to that later. So it'll be called user model assembler. And the idea is that there's gonna be many places in the model where I need to contribute and assess the links that are viable for that thing in the model, right?

[00:01:49]
So these are assemblers, you can reuse these all over the code and they do that work for you. So you just get used to like, you know, calling them or invoking them. So I'm gonna call to_model, okay? There you go. And my job is to build out this thing right here.

[00:02:05]
So var controller is equal to. What did I call this? Let's call this, okay, Media controller or users controller. Put that in there, okay? And then here, that's the controller name. The link to self is equal to LinkTo, okay, method on controller. And I've only got one method, so I'll call that the self link.

[00:02:44]
But now imagine I had a method for just one. I don't, but imagine I did. Okay, so okay, there we go. So it's just one, and we can do usersClient.by_id. Okay, and then I do model assembler. We'll come back to that In a second. So now I have another link here to 1, and I can say 1 passing in the ID for the entity, okay?

[00:03:14]
So this would be called, actually withRel(all), that'll be called 1 or self. And then I'm going to return an entity model. So entity model of entity self one. Okay, so now I can use this component in my controller when I want to produce a response of type entity model back to the controller.

[00:03:34]
There we go. This is here, final. There we go. Good, okay, so go there. Now I go here. I'm gonna create the User Model Assembler.toModel, this one. Good, and then here .toCollection model usersClient.users, okay? So what's the issue with this one now? It's a collection model of entity.

[00:04:10]
Yeah, yeah, there you go. So, it's basically the same code, but I'm wrapping it in this model envelope, okay? And the model envelope has links attached to it. Okay, so now what happens if I go here? Nothing, I didn't configure a menu. But if I go here, you can see now there's links, there's metadata attached in the JSON payload that shows me the navigable useful endpoints here.

[00:04:38]
And I'm doing it based on this model assembler. Dynamically, I'm saying when somebody calls this method on my controller of this type all, then that's called the all link. When they call this one, that's called self. That's my current one. And you can do some. If you can say if entity deleted is suspended or whatever, and then put different links in there so you can dynamically show more or less of the links based on the state of the entity.

[00:05:06]
So the server side drives the representation of the state on the client side here. So that's called hypermedia. We could get into the whole thing. You really should, but I gotta move on here quickly. Any questions on that one, though? You can see it's got an embedded list here.

[00:05:21]
So if I go down to the bottom. Yeah, look at that. I could actually add links to the resource as well, but I didn't. And this is HAL, this is hypertext application links or whatever. There's other encodings as well, for this, so lots of good stuff.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now