Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's talk about the project we are going to build in this workshop.
Prerequisites
Related (Dejà Vu) API courses
Get the source code
This source code is available on GitHub
-
0:00
[MUSIC]
-
0:04
Hello and welcome.
-
0:06
I'm Craig, and I'm a developer.
-
0:07
In this course, we're going to build a RESTful API using Spring.
-
0:11
It's going to support HATEOAS,
-
0:13
or Hypermedia as the Engine of Application State.
-
0:16
It's gonna integrate with Spring data and Spring security, and
-
0:19
provide self-documenting schema generation, almost seamlessly.
-
0:24
Now, word of warning.
-
0:25
You're probably going to get your mind blown by how much you can do with such
-
0:28
little code, and that's the beauty of Spring Boot at work.
-
0:32
But, before we take off,
-
0:33
please take a moment to check out the teacher's notes and make sure that you've
-
0:36
completed the prerequisites, just to make sure we're talking the same language.
-
0:40
Don't forget to check the notes on each video.
-
0:42
I usually drop helpful hints, tricks, and knowledge nuggets down there for
-
0:46
your consumption.
-
0:47
And, always remember the speed controls.
-
0:49
Feel free to speed me up or slow me way down.
-
0:53
I don't mind at all.
-
0:54
So let's talk about the project that we're gonna build together.
-
0:57
Now first off, imagine, if you will, that we work on an international team
-
1:01
that produces applications as requested by clients, you know, like an agency.
-
1:05
So we have a very well established client who has come to us and
-
1:09
asked us to build a course review system.
-
1:11
They want a single-page application website, an iPhone and Android app, and
-
1:15
of course, they want it yesterday, and our sales team told them it'd be no problem.
-
1:20
So, here we go.
-
1:22
So first off, a course review system is reviewing online courses, you know,
-
1:25
like this one you're watching here.
-
1:27
It'll be used to gather public opinion on those courses, so
-
1:30
people can make an educated decision on what they should watch.
-
1:33
Our team huddled together and
-
1:34
decided that we should tackle building out all the prototypes at the same time.
-
1:38
The approach that we came up with to do this joint team rapid development
-
1:42
is to produce a REST API, that all clients, you know,
-
1:44
the site and the different phone and tablet apps can consume.
-
1:47
So you've landed on the API creation team, arguably,
-
1:50
the most important team on getting this project launched, so good job.
-
1:54
So together,
-
1:55
we're going to build an API that allows anyone to submit courses to it, then we'll
-
1:59
also allow anyone to add reviews about the courses that have been submitted.
-
2:03
So, for instance, if someone wanted to add a review
-
2:06
to this workshop that you're watching right now, they'd do something like this.
-
2:09
So we have the client let us know the title and the URL.
-
2:11
Our project manager, her name's Hannah,
-
2:14
has assured us that that should be enough to get us started.
-
2:17
And now that this course that you are currently watching exists already,
-
2:20
we could have people submit reviews to it.
-
2:22
Now I know you haven't seen this yet, so these probably won't make too much sense,
-
2:25
but here are some reviews it's gotten.
-
2:27
So this has a rating of four out of a possible five and
-
2:29
a more detailed description, and someone else submitted this one.
-
2:33
That's not how you pronounce HATEOAS!
-
2:35
Well, hatters gonna hat, I guess.
-
2:37
So side note, if you feel like you're having deja vu, don't worry,
-
2:40
you might have seen this project used in another one of our courses here on
-
2:43
Treehouse, on a different web framework.
-
2:45
We're producing several workshops and courses in various other frameworks so you
-
2:49
can compare and contrast their approaches to building the same end product.
-
2:53
So the relationship is one course to many reviews.
-
2:57
One to many.
-
2:59
We'll also add the ability to walk from the review back to the course.
-
3:03
So that's many to one, right, from the perspective of the review.
-
3:06
It's many reviews to one course.
-
3:09
Now not only will our resource endpoints support all the HTTP verbs,
-
3:14
it will also be hypermedia driven.
-
3:16
It will be HATEOAS compliant.
-
3:18
Again, that's the Hypermedia as the Engine of Application State.
-
3:21
It's gonna be discoverable.
-
3:22
Someone can make a request, and our API will produce a list of all of its defined
-
3:27
relationships so that it can see what other actions are possible and
-
3:31
make a new request to other related resources.
-
3:34
For instance, when looking at a specific review,
-
3:36
it would have a relation link back to the course resource.
-
3:40
Now with the skills that you've picked up thus far, you could definitely build
-
3:43
a Spring MVC application that took care of all of this, right?
-
3:46
For example, we could build out our CourseController, and then we'd write some
-
3:50
handlers, and we could have some code that respond to an HTTP getRequest
-
3:56
to the courses endpoint, which would return a list of all available courses.
-
3:59
Yeah, we'd also need to convert those Java objects to JSON,
-
4:03
or JavaScript Object Notation, you know, the standard, so
-
4:05
that our clients can consume them.
-
4:07
And we'd also add one that creates a new course when the courses collection
-
4:11
is posted to.
-
4:12
We'll have to parse the JSON that was sent in and there's plenty of great tools for
-
4:16
that, many embedded in the Spring framework.
-
4:18
Now, as you've probably experienced from working with Spring,
-
4:21
it's the framework that keeps on giving.
-
4:23
Since this is a pretty repetitive task,
-
4:25
there are a lot of tools in Spring that help ease these monotonous tasks.
-
4:30
For instance,
-
4:30
there's an annotation that allows you to mark a controller as a REST controller.
-
4:34
It is, as you might expect.
-
4:36
So now, by default, you can return domain objects and they will be converted to
-
4:41
JSON, using a tool included in your Spring installation called Jackson.
-
4:45
We'll take a look at that here in a bit.
-
4:47
So that's great, but still, those controllers are probably going to end up
-
4:50
looking a lot like each other, right?
-
4:53
I mean, just about every application you can think of would work
-
4:55
pretty much like this, wouldn't it?
-
4:57
List of stuff, get the details of the stuff,
-
4:59
add more stuff, add some stuff that's related to that stuff, delete some stuff.
-
5:03
Sure, why not.
-
5:05
That's pretty repetitive, and believe me, code like this actually exists, and
-
5:09
you'll encounter it all over the place in all sorts of frameworks.
-
5:12
REST API Creation is usually a repetitive error prone task, and
-
5:15
when you introduce the concept of resource linking,
-
5:18
it quickly starts to become overwhelming in complexity.
-
5:22
There's so much to remember!
-
5:24
What standard should I adhere to?
-
5:26
There's got to be a better way, right?
-
5:28
I mean, if it's all doing the same process,
-
5:30
which is basically just exposing your data model to a client in the same format,
-
5:34
why should we developers have to write that over and over again?
-
5:38
I mean, we have better stuff to do.
-
5:39
We've got to change the world with our awesome app.
-
5:41
Now, what if I told you that Spring developers agreed with that
-
5:44
thought process and created a way to make sure that you
-
5:47
actually don't even have to write the controller piece at all.
-
5:50
Wouldn't that be awesome?
-
5:51
Well you've come to the right workshop, my friend.
-
5:53
Sit tight, here it comes.
You need to sign up for Treehouse in order to download course files.
Sign up