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