This workshop will be retired on May 1, 2025.
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 discuss and get Spring Data REST installed.
Gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath('org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE')
}
}
apply plugin: 'java'
apply plugin: 'spring-boot'
compile 'org.springframework.boot:spring-boot-starter-data-rest'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
Read More
- Spring Data REST project page
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
As you know Spring Boot takes a very
opinionated approach to configuration of
0:00
your web applications.
0:04
If you don't override the configuration
what will get set up automatically is
0:05
probably exactly what you wanted or at
least that's the claim of the framework.
0:09
I find the convention over configuration
approach is exactly what I want.
0:13
Almost all of the time I've always been
thoroughly impressed with what you get out
0:17
of the box from a Spring Boot project.
0:21
And as you saw in spring data
similar auto magic things happen.
0:24
You simply define an interface and
make it match a naming structure and
0:27
blammo you have a working
method that queries or
0:31
entities whatever back in
database you might choose.
0:33
It's super powerful, extremely intuitive
and such an amazing time saver.
0:36
Well there is a project that
works in a similar start simple
0:41
add complexity of events.
0:43
Spring data rest is quite literally
the combination of both of these projects
0:45
and a few more.
0:49
Its mission is to take the repetition
out of building a REST API.
0:50
Much like how Spring Boot assumes
that if you put a vendor's database
0:54
in your class path it means that you want
to have your app configured to use it.
0:58
Spring data rest assumes that
if you've built a repository
1:02
that you intend to expose it as
a rest collection and resource.
1:05
You can of course change that
assumption but it's created for
1:09
you in the most common use
case that you're gonna need.
1:12
There's another project
in the Spring family and
1:15
that's called Spring HATEOAS which
provides, as you might have guessed,
1:17
APIs that help you build
discoverable rest representations.
1:22
Spring Data REST has this project
installed and turned on by default.
1:26
This means it will automatically generate
all the links and representations.
1:30
We'll walk through all
those things here in a bit.
1:34
And I realize that I sound like one of
those sketchy infomercials actors and
1:36
you're waiting for the catch.
1:40
But wait there's more, not only does it
automatically produce a rest API it slices
1:41
POJO that dices JSON,
it serves of schemas.
1:45
Now one of the problems in behind
the scenes magic that this type of library
1:48
produces is that most of the time you
have no idea what's being automated.
1:52
Now I'll do my best to point out
what is happening behind the curtain
1:55
while not distract you from how
to use this lifesaving tool to
1:59
rapidly build REST full APIs.
2:01
Like that sketchy information
person might offer you
2:04
come on in take a test drive no
obligation, money back guarantee.
2:07
And it's a free and open source so.
2:10
All right so
let's get this thing installed.
2:13
So I'm going to make a new
gradle project an IntelliJ IDEA.
2:15
So over here I've got Gradle selected it's
got my version, I'm gonna click Next.
2:19
And for the Groupid were gonna do
our favorite com.teamtreehouse.
2:23
And the Artifactid, let's have our
jar be named course-reviews-api.
2:28
Okay, and on this page, let's make sure
that we check create directories for
2:37
empty content routes automatically, so
that those directories get built for
2:41
us and we don't need to do it, awesome.
2:44
Choose Next, and
then finally that looks good.
2:46
Great, okay, so
let's install our dependencies.
2:51
So let's open up our build.gradle
file which is right here.
2:54
And I'm going to apply the suggestion so
that the gradle wrapper comes along.
2:59
So the first thing I'm gonna do is I'm
going to change this 1.5 to 1.8 so
3:05
that we're using Java 8, right?
3:09
Now we definitely want to include
the spring boot great all plugins.
3:11
So we get the auto versioning
of our imports, right?
3:16
So to do that what you need to do first is
you need to add a build script up here.
3:19
So we're gonna say buildscript,
make the new
3:22
closure there and
the repositories section and
3:27
on mavenCentral there is a plugin
that we're gonna wanna use.
3:33
So classpath and
here is org.springframework.
3:40
This is all in the teacher's
notes by the way,
3:47
boot spring-boot-gradle-plugin.
3:51
And at the time of this video,
we are 1.3.3.RELEASE,
3:55
I will try to keep that updated.
4:00
Okay, great so
now we have a build script closure so
4:05
now what that means,
is we can apply the plugin.
4:07
So we have a new plug in to apply.
4:11
Apply plugin and
the name of that is spring-boot.
4:14
That brings along awesome stuff where
we don't need to say the version of
4:18
the dependency that we're talking about.
4:21
So the dependency that
we wanna add here is we
4:23
wanna say compile and
we want to bring across
4:28
org.springframework.boot:spring-boot-star-
ter-data-rest.
4:34
So it's spring-data-rest is the project.
4:43
And we want to have a database set up so
let's set that up first.
4:48
Let's make sure that we're
using spring-data so we want
4:53
org.springframework.boot:spring-boot-star-
ter-data and
4:58
we're gonna use the JPA probably look
at that I could just copy that line.
5:04
Okay, so let's have our app
use an H2 in memory database.
5:11
Here is one of those, I can't believe
this is all you have to do moments.
5:17
Are you ready for this?
5:21
Check this out, all you do is
add the dependency compile and
5:22
that is com.h2database:h2.
5:27
Whoa, right?
5:32
Okay, and so let's go over here and let's
refresh our Gradle project make sure that
5:34
we've got everything working, takes
a little bit the first time you do this.
5:38
It's doing all the downloads and
sorts everything out.
5:42
Okay, and I had a little unindexed
remote maven imposed profound.
5:44
Let's go ahead and click this
that will get indexed next time.
5:48
Okay, and let's expand our
dependencies and let's take a look.
5:51
Whoa, look at all those, wow.
5:54
So what do you say that we
make use of some of these?
5:55
Let's take a quick break and then let's
start building out our resources.
5:59
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