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